100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript

while and do-while Loops in JavaScript

Understand condition-controlled looping with while and do-while, including the crucial difference in when the condition is checked and how to avoid infinite loops.

Control FlowBeginner8 min readJul 8, 2026
Analogies

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

javascript
// 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

javascript
// 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

text
after while, count = 5
do-while iteration 5
after do-while, n = 6

6. 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

Was this page helpful?

Topics covered

#JavaScript#JavaScriptProgrammingStudyNotes#Programming#WhileAndDoWhileLoopsInJavaScript#While#Loops#Syntax#Explanation#StudyNotes#SkillVeris