loops
loops
condition is met. There are several types of loops in JavaScript, each serving
different purposes. Here are the most commonly used loops:
1. For Loop
The for loop is used when you know in advance how many times you want to
execute a statement or a block of statements.
Syntax:
javascript
Example:
javascript
2. While Loop
The while loop executes a block of code as long as the specified condition is true.
Syntax:
javascript
Example:
javascript
3. Do...While Loop
The do...while loop is similar to the while loop, but it guarantees that the block of
code will be executed at least once, even if the condition is false.
Syntax:
javascript
Example:
javascript
4. For...of Loop
The for...of loop is used to iterate over iterable objects (like arrays, strings, etc.).
Syntax:
javascript
Example:
javascript
VerifyOpen In EditorRunCopy code
1const array = [10, 20, 30];
2for (const value of array) {
3 console.log(value); // Outputs: 10, 20, 30
4}
5. For...in Loop
The for...in loop is used to iterate over the properties of an object.
Syntax:
javascript
Example:
javascript