Category: javascript
loops
Published on 29 Jun 2026
Explanation
Loops execute a block of code repeatedly until a specified condition is met.
Code:
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Explanation
The while loop continues executing as long as its condition remains true.
Code:
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
Explanation
The do...while loop executes the code at least once before checking the condition.
Code:
let i = 1;
do {
console.log(i);
i++;
} while (i <= 5);
Explanation
The for...of loop iterates over arrays and other iterable objects.
Code:
const colors = ['Red', 'Blue'];
for (const color of colors) {
console.log(color);
}
Explanation
The break statement exits a loop early, while continue skips the current iteration.
Code:
for (let i = 1; i <= 5; i++) {
if (i === 3) continue;
console.log(i);
}