Last updated: August 27, 2026.
JavaScript loops repeat a block of work. Choose the loop form that makes the stopping condition and the value being processed easy to understand.
Loop through array values with for…of
const products = ["Keyboard", "Mouse", "Monitor"];
for (const product of products) {
console.log(product);
}for...of is usually the clearest choice when you need each value from an array or another iterable.
Use a counter with for
const quantities = [2, 5, 3];
let total = 0;
for (let index = 0; index < quantities.length; index += 1) {
total += quantities[index];
}
console.log(total);A traditional for loop is useful when the index matters or when you need precise control over the counter.
Repeat while a condition is true
let attempts = 0;
let connected = false;
while (!connected && attempts < 3) {
attempts += 1;
connected = tryConnection(attempts);
}
function tryConnection(attempt) {
return attempt === 3;
}break and continue
for (const value of [4, -1, 7, 0, 9]) {
if (value < 0) continue;
if (value === 0) break;
console.log(value);
}continue skips the rest of the current iteration. break exits the nearest loop.
See MDN’s loops and iteration guide for every loop statement.