4 min read
while Loops
Prerequisites: Loops
A while loop keeps running its block as long as a condition stays
true — used when you don't know in advance how many times you'll
need to repeat.
let count = 0;
while (count < 3) {
console.log(count);
count++;
}
Expected output
0
1
2
Common mistakes
- Forgetting to update the loop's condition variable (
count++above) — this creates an infinite loop that never stops and can freeze a browser tab.