Loops (for & while)
Learn how loops repeat code automatically — for loops for counted repeats and while loops for conditional ones.
What you'll learn
- Why loops exist and what they replace
- How a for loop counts through a range
- How a while loop repeats until a condition fails
- How to avoid infinite loops
Concept
Loops (for & while)
Loops repeat a block of code multiple times. Without loops, you would have to copy and paste the same lines for every repetition — imagine printing "Hello" 100 times by hand. Loops let you write the instruction once and say "do this N times."
The for Loop
A for loop is used when you know how many times to repeat. It has three parts: initialization, condition, and update.
for (let i = 0; i < 5; i++) {
console.log("Iteration", i);
}
// Prints: Iteration 0, 1, 2, 3, 4
let i = 0— runs once, before the loop starts.i < 5— checked before each iteration; the loop continues while true.i++— runs after each iteration, incrementing the counter.
The while Loop
A while loop is used when you do not know how many repetitions you need — you repeat until a condition becomes false.
let dice = 0;
while (dice !== 6) {
dice = Math.floor(Math.random() * 6) + 1;
console.log("Rolled:", dice);
}
Beware Infinite Loops
An infinite loop never stops because its condition never becomes false. This freezes the program. Always make sure the loop's progress eventually breaks the condition — for example, that i increases toward the limit, or that dice can actually reach 6.
Use break to exit a loop early and continue to skip to the next iteration when you need finer control.
Example
// for loop: count down from 5
for (let i = 5; i > 0; i--) {
console.log(i);
}
console.log("Liftoff!");
// while loop: double a number until it exceeds 100
let value = 1;
let steps = 0;
while (value <= 100) {
value *= 2;
steps++;
}
console.log(`Reached ${value} in ${steps} steps`); // Reached 128 in 7 steps
The for loop counts down because i-- decreases i each step. The while loop keeps doubling until the condition (value <= 100) is false.
Try it
- Crontab Generator
Cron expressions are essentially loops in time — 'run every N minutes'. See how repeating schedules work.
Common mistakes
The mistake
Writing a while loop whose condition can never become false.
The fix
Ensure the loop body changes a variable used in the condition. Mentally trace: does progress happen every iteration?
The mistake
Off-by-one errors: using < instead of <=.
The fix
Decide whether the endpoint is inclusive. for (let i = 0; i <= n; i++) runs n+1 times; i < n runs n times. Test the boundary.
Related Resources
Related Snippets
- JavaScript Array Methods
Array methods like forEach and map are loops in disguise.
Practice Loops (for & while)
2 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.