1. Introduction
while and do-while loops repeat a block of code as long as a condition remains true, without requiring you to know in advance how many iterations are needed. They are the natural choice when the stopping condition depends on something that changes unpredictably during execution, such as reading input until a sentinel value appears, or retrying an operation until it succeeds.
Cricket analogy: A bowler keeps bowling overs at a batter in the nets not for a fixed count but until the batter finally middles a cover drive cleanly, since nobody knows in advance how many balls that will take.
JavaScript provides two variants: while, which checks its condition before running the loop body (so the body may run zero times), and do-while, which checks its condition after running the body (so the body always runs at least once).
Cricket analogy: A third umpire checks the replay before signaling a boundary is out (like while, condition first) versus a fielder who dives and only afterward realizes the ball had already crossed the rope (do-while, action then check).
2. Syntax
// while: condition checked BEFORE each iteration
while (condition) {
// may run zero times if condition starts false
}
// do-while: condition checked AFTER each iteration
do {
// always runs at least once
} while (condition);3. Explanation
In a while loop, JavaScript evaluates the condition first; if it is falsy on the very first check, the loop body never executes at all. In a do-while loop, the body executes once unconditionally, and only then is the condition checked to decide whether to repeat — so a do-while loop's body is guaranteed to run at least once even if the condition is false from the start.
Cricket analogy: If the umpire checks the light meter before play and it's too dark, the innings never starts that session, but a batter who always walks out for the toss first only afterward learns if rain will wash out the day.
Both loop types rely entirely on you to update whatever variable the condition depends on inside the loop body. Unlike the classic for loop, there is no built-in update step, so it is easy to forget to change the condition variable.
Cricket analogy: Just as a scorer must manually update the run tally after every ball or the scoreboard freezes, a while loop's condition variable must be manually updated inside the body or the loop never knows the match has progressed.
The most common bug with while and do-while is the infinite loop trap: if the code inside the loop never changes the value the condition depends on (or changes it in a way that never makes the condition false), the loop runs forever and can freeze or crash the program. Always double-check that every code path inside the loop body moves the condition toward becoming false, and be especially careful with do-while, since even a false condition doesn't prevent the first execution.
4. Example
// while: condition false from the start, body never runs
let count = 5;
while (count < 3) {
console.log('while iteration', count);
count++;
}
console.log('after while, count =', count);
// do-while: body runs once even though condition is false from the start
let n = 5;
do {
console.log('do-while iteration', n);
n++;
} while (n < 3);
console.log('after do-while, n =', n);5. Output
after while, count = 5
do-while iteration 5
after do-while, n = 66. Key Takeaways
- while checks its condition before each iteration, so the body may run zero times.
- do-while checks its condition after each iteration, so the body always runs at least once.
- Neither loop has a built-in update step — you must manually change the condition variable inside the body.
- Forgetting to update the condition variable (or updating it incorrectly) causes an infinite loop.
- Use while when the body might not need to run at all; use do-while when the body must run at least once regardless of the condition.
Practice what you learned
1. What is the key difference between while and do-while regarding when the condition is checked?
2. Given `let count = 5; while (count < 3) { console.log(count); count++; }`, how many times does the loop body execute?
3. Given `let n = 5; do { console.log(n); n++; } while (n < 3);`, how many times does the loop body execute?
4. What is the most common cause of an infinite while loop?
5. Which loop type is best suited for a scenario where you must attempt an operation at least once before checking whether to retry, such as prompting a user for input and validating it afterward?
Was this page helpful?
You May Also Like
for Loop in JavaScript
Master the classic for loop along with for...in and for...of, and understand the key differences between iterating indexes, keys, and values.
break and continue in JavaScript
Learn how to alter loop execution with break and continue, and how labeled statements let you control outer loops from inside nested loops.
if-else in JavaScript
Learn how to make decisions in JavaScript using if, else if, and else statements, and understand how truthy/falsy values control branching.
let, const and Block Scope in JavaScript
How let and const introduced block scoping, the temporal dead zone, and why const does not make objects immutable.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics