How to Use a While Loop in JavaScript
Use a while loop when code should keep running until a condition changes, but the number of repetitions is not known in advance. This works best for retries, polling, game loops, and input-driven flows.
What you’ll build or solve
You’ll learn how to use a while loop in JavaScript with a condition and a state update. You’ll also know how to avoid accidental infinite loops.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when repetition depends on changing state instead of a fixed count.
Common real-world scenarios include:
- Retry logic
- Polling APIs
- Input validation loops
- Game state loops
- Reading queue items
This is a bad idea when the number of repetitions is already known. In that case, a for loop is usually cleaner.
Prerequisites
You only need:
- A JavaScript file or browser console
- Basic variables and conditionals knowledge
Step-by-step instructions
Step 1: Write the condition and update the state inside the loop
Start with the condition inside parentheses.
JavaScript
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
The loop keeps running while the condition stays true.
The state update inside the loop is what eventually stops it.
This works well for dynamic conditions.
JavaScript
let isLoading = true;
while (isLoading) {
checkStatus();
isLoading = false;
}
What to look for:
- The condition is checked before every cycle
- Update the state inside the loop
- Great when repetition count is unknown
- Missing updates cause infinite loops
- Use
breakfor emergency exits
Examples you can copy
Retry attempts
JavaScript
let attempts = 0;
while (attempts < 3) {
attempts++;
}
Queue processing
JavaScript
while (queue.length > 0) {
queue.shift();
}
Countdown
JavaScript
let timer = 5;
while (timer > 0) {
console.log(timer);
timer--;
}
Common mistakes and how to fix them
Mistake 1: Forgetting to update the state
What the reader might do:
JavaScript
let count = 0;
while (count < 5) {
console.log(count);
}
Why it breaks: count never changes, so the loop never ends.
Corrected approach:
JavaScript
count++;
Mistake 2: Using the wrong condition direction
What the reader might do:
JavaScript
while (timer < 0)
Why it breaks: the loop may never start.
Corrected approach:
JavaScript
while (timer > 0)
Mistake 3: Using while for known fixed counts
What the reader might do:
JavaScript
let i = 0;
while (i < 10) {
console.log(i);
i++;
}
Why it breaks: this works, but for is clearer for fixed repetition.
Corrected approach:
Use for when the count is known.
Troubleshooting
If the loop freezes, check whether the state changes inside the block.
If the loop never starts, inspect the initial condition.
If the wrong number of cycles runs, log the changing variable.
If the count is fixed, switch to for.
Quick recap
- Use
whilefor unknown repetition counts - Put the condition in parentheses
- Update the state inside the loop
- Watch for infinite loops
- Use
forfor fixed counts
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot