In JavaScript, there are several types of loops for performing repetitive actions:
for
: Best suited for when the number of iterations is known beforehand. It has three expressions: initialization, condition, and increment.while
: Executes code as long as a condition is true. The condition is checked before the loop body is executed.do...while
: Similar to while
, but guarantees that the loop body will be executed at least once, as the condition is checked after the iteration.for...in
: Iterates over the keys (properties) of an object. Not recommended for arrays.for...of
: Iterates over the values of iterable objects, such as arrays, strings, Map
, and Set
. This is the modern and preferred way to iterate over arrays.for
loopThe classic loop, ideal for situations where the number of iterations is known.
Syntax:
for (initialization; condition; increment) {
// loop body
}
let i = 0
).true
, the loop continues.i++
).Example:
// Output numbers from 0 to 4
for (let i = 0; i < 5; i++) {
console.log(i); // 0, 1, 2, 3, 4
}
while
loopUsed when the number of iterations is not known in advance, and the loop should continue as long as a specific condition is met.
Syntax:
while (condition) {
// loop body
}
Example:
let i = 0;
while (i < 5) {
console.log(i); // 0, 1, 2, 3, 4
i++;
}
Feature: If the condition is initially false, the loop will not execute at all.
do...while
loopGuarantees that the loop body will be executed at least once, as the condition check occurs after the iteration.
Syntax:
do {
// loop body
} while (condition);
Example:
let i = 5;
do {
console.log(i); // 5
i++;
} while (i < 5);
In this example, the loop body executes once, even though the condition i < 5
is initially false.
for...in
loopDesigned for iterating over the enumerable properties of an object.
Syntax:
for (let key in object) {
// loop body
}
Example:
const user = {
name: "Alice",
age: 30,
isAdmin: true
};
for (let key in user) {
console.log(`${key}: ${user[key]}`);
}
// Output:
// name: Alice
// age: 30
// isAdmin: true
Features:
for...of
loopThe modern standard for iterating over iterable objects (arrays, strings, Map
, Set
, etc.).
Syntax:
for (let value of iterable) {
// loop body
}
Example with an array:
const fruits = ["Apple", "Banana", "Cherry"];
for (let fruit of fruits) {
console.log(fruit);
}
// Output:
// Apple
// Banana
// Cherry
Example with a string:
const greeting = "Hello";
for (let char of greeting) {
console.log(char);
}
// Output:
// H
// e
// l
// l
// o
Loop | When to Use | Features |
---|---|---|
for | When the number of iterations is known. | Full control over the process (index, step). |
while | When the number of iterations is unknown. | Checks the condition before execution. |
do...while | When the body needs to run at least once. | Checks the condition after execution. |
for...in | For iterating over object keys. | Not for arrays; order is not guaranteed. |
for...of | For iterating over values of iterables. | Simple syntax; ideal for arrays and strings. |