Loop with for, while, and do...while. Iterate over iterables with for...of and object keys with for...in. Control flow with break and continue.
Why: for is the most common loop when the number of iterations is known. while loops when a condition drives iteration. do...while always executes the body at least once.
// for loop — log squares 1–5
for (let i = 1; i <= 5; i++) {
console.log(`${i}² = ${i ** 2}`);
}
// while
let count = 3;
while (count > 0) {
console.log('count:', count);
count--;
}
// do...while — body runs at least once even if condition is false
let n = 0;
do {
console.log('ran with n =', n);
n++;
} while (n < 1);
console.log('final n:', n); // 1Why: for...of iterates values of any iterable (array, string, Map, Set). for...in iterates enumerable keys of an object. break exits; continue skips to the next iteration.
// for...of — values of iterables
const colors = ['red', 'green', 'blue'];
for (const color of colors) {
console.log(color);
}
// Works on strings too
for (const char of 'abc') {
console.log(char);
}
// for...in — enumerable keys of objects
const person = { name: 'Alice', age: 30 };
for (const key in person) {
console.log(key, '->', person[key]);
}
// break and continue
for (let i = 0; i < 10; i++) {
if (i === 3) continue; // skip 3
if (i === 6) break; // stop at 6
console.log(i); // 0 1 2 4 5
}