for (let i = 0; i < 5; i++): a fixed number of times.
while (condition): while a condition is true.
do { } while (condition): runs at least once.
for (const item of array): each value of an iterable (arrays, strings).
for (const key in object): each key of an object.
break exits a loop early; continue skips to the next iteration.
💡 Key Takeaways: JavaScript Loops
Mastering JavaScript Loops helps you write clean and reliable JavaScript code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.
JavaScript Loops Code Example
JavaScript
<pre id="out"></pre>
<script>
const log = (...a) => document.getElementById("out").textContent += a.join(" ") + "\n";
for (let i = 1; i <= 5; i++) {
log("7 x", i, "=", 7 * i);
}
let n = 3;
while (n > 0) { log("Countdown", n); n--; }
for (const lang of ["HTML", "CSS", "JS"]) log("Learning", lang);
const scores = { ali: 80, sara: 92 };
for (const name in scores) log(name, "→", scores[name]);
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) continue;
if (i > 7) break;
log("odd:", i);
}
</script>
Free sign-in required · Google login available
JavaScript Loops Practice Exercises
Check what you learned in “JavaScript Loops”.
/ 3 solved
Fill in the blank
Loop over each value of the array.
for (const item items) { log(item); }
✓ Correct!Not quite. Try again.
Multiple choice
Which statement skips to the next loop iteration?
✓ Correct!Not quite. Try again.
Code challenge
Use a loop to log the numbers 1 to 5, each on its own line.
Write the code in the live editor. Your answer is checked automatically.