iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
A JavaScript while loop checks its condition before every pass. If the condition is truthy, the body runs; if it is falsy, the loop stops and the program continues after the closing brace. That single rule explains both how a while loop behaves and why it sometimes never ends.
The basic pattern
The syntax is:
while (condition) {
// work to repeat
}
The condition is an expression. JavaScript converts its result to a boolean, so values such as true, non-zero numbers, and non-empty strings count as truthy, while false, 0, "", null, undefined, and NaN count as falsy. The body can be a single statement without braces, but braces are the safer habit because they group several statements together.
Execution order, step by step
MDN describes the behavior directly: “The condition is evaluated before executing the statement” (MDN Web Docs, “while – JavaScript”). Every pass follows the same cycle:
- JavaScript evaluates the condition.
- If the result is falsy, the loop ends and execution moves to the next statement after the loop.
- If the result is truthy, the body runs once from top to bottom.
- Control returns to step 1.
Tracing a counter example
Here is a short loop you can run in a browser console or Node.js:
#1 Best Overall
let count = 0;
while (count < 3) {
console.log(count);
count++;
}
console.log("Done. count is", count);
Trace it one pass at a time:
| Check | count before check |
count < 3 |
What happens |
|---|---|---|---|
| 1 | 0 | true | Logs 0, then count becomes 1 |
| 2 | 1 | true | Logs 1, then count becomes 2 |
| 3 | 2 | true | Logs 2, then count becomes 3 |
| 4 | 3 | false | Loop ends; the final log prints Done. count is 3 |
The body runs three times, and count ends at 3. Notice that the update count++ is what moves the value toward the point where the condition fails.
A false condition means zero passes
Because the check happens first, a loop whose condition starts out false never runs its body:
Rank #2
let count = 5;
while (count < 3) {
console.log("This never prints");
count++;
}
Do not assume a while loop runs at least once. That behavior belongs to do...while, covered below.
Recommended Free Tools
How to make sure the loop ends
A while loop keeps going as long as its condition stays truthy. To guarantee an end, check two things:
- The condition depends on a value. Name the variable it tests, such as
countincount < 3. - Something inside the body changes that value so the condition can eventually become falsy, or the body contains a reachable
break.
If neither holds, the loop runs forever and the page or process freezes. This broken version looks almost identical to the working one:
let count = 0;
while (count < 3) {
console.log(count);
// forgot count++
}
The condition is always true because count stays at 0. In a browser tab, this blocks the page; in Node.js, it stops the script from finishing. Stop it with the browser’s stop control or Ctrl+C in a terminal.
Rank #4
Troubleshooting an infinite loop
- Write down the condition and the variables it reads.
- Find every place in the body where those variables change. If there is none, add the update.
- Check that the update moves in the right direction.
count--withcount < 3starting at 0 never reaches 3 going down, so the loop still runs forever. - Temporarily add
console.loginside the body to print the variable on each pass.
Controlling a loop with break and continue
break exits the nearest enclosing loop immediately. Statements after it in the same pass are skipped, and the program continues after the loop:
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →let n = 0;
while (true) {
n++;
if (n === 4) {
break; // leaves the loop now
}
console.log(n);
}
// logs 1, 2, 3
continue ends the current pass early and returns control to the condition check. It does not leave the loop:
Best Value
let n = 0;
while (n < 5) {
n++;
if (n === 3) {
continue; // skip logging 3, then check n < 5 again
}
console.log(n);
}
// logs 1, 2, 4, 5
A labeled break can exit an outer loop from inside a nested one, but a plain break only affects the innermost loop. Use labels sparingly, because they are easy to misread for beginners.
Choosing between while, for, and do…while
| Form | When the condition is checked | Minimum passes | Best fit |
|---|---|---|---|
while |
Before each pass | Zero | Repeat while a condition holds, when the number of passes is not known in advance |
for |
Before each pass, with initialization and update kept on one line | Zero | Counting through a known range, where initialization, test, and update are easy to scan together |
do...while |
After each pass | One | The body must run at least once before the first check, such as prompting until valid input |
These are readability choices rather than a ranking. A counter loop can be written with any of the three, and the best form is the one that makes the stopping rule easiest to see.
Quick checklist before you run a while loop
- The condition tests a specific variable.
- The body changes that variable in the direction that makes the condition false.
- You are fine with the body running zero times if the condition starts false.
- Any
continueis placed after the update, or the update is done before it, so the loop still progresses.
The MDN while reference and its loop guide are the primary references for the behavior described here; both use the same pre-test order shown above.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

