Loops in JavaScript

From the JavaScript cheat sheet ยท Control Flow ยท verified Jul 2026

Loops

for, while, do-while, for...of, for...in, break, and continue

javascript
for (let i = 0; i < 5; i++) { }
while (condition) { }
for (const item of array) { }    // iterate values
for (const key in object) { }    // iterate keys
๐Ÿ’ก for...of iterates VALUES (arrays, strings, Maps, Sets) โ€” use for most loops
โšก for...in iterates KEYS/properties (objects) โ€” avoid on arrays, use for...of instead
๐Ÿ“Œ break exits the loop entirely; continue skips to the next iteration
๐ŸŸข for...of works on any iterable โ€” arrays, strings, Maps, Sets, generators

More JavaScript tasks

Back to the full JavaScript cheat sheet