JavaScript While Loop
for packs start, condition, and update into one header. Sometimes you only know the rule “keep going while this is true” — and the counter changes inside the work itself. A while loop checks one condition up front, runs the body, then checks again. Same idea of repeating work; different shape on the page.
What does while do?
while runs its block again and again as long as the condition in parentheses stays true. There is no init or update slot in the header. You prepare variables before the loop, and you change them inside the body so the condition can eventually become false. When it does, the loop stops and the next statement after the loop runs.
while, for, and do...while at a glance
📊 Everyday loop shapes
| Shape | When it fits | Condition check |
|---|---|---|
while |
keep going while a rule holds; update lives in the body | before each round |
for |
you already know start, stop, and step | before each round (three-part header) |
do...while |
body must run at least once | after each round |
Most beginner while examples still use a small integer counter. The difference is placement: you write let i = 0 before the loop and i++ (or another change) inside the braces. If you never change what the condition reads, the loop may never end.
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
Count with while
i starts at 0 before the loop. Each round prints i, then adds one. You see 0, 1, 2, 3, 4 — five rounds. The value 5 never prints because the condition fails first. Without i++ inside the body, i would stay 0 forever.
Read one full round slowly: check condition → run the body (including the update) → check again. That is why forgetting the update is the classic infinite-loop trap with while.
let n = 1;
while (n <= 3) {
console.log("step " + n);
n++;
}
Clearer labels from 1
Here the counter starts at 1 and the condition uses n <= 3, so you see step 1, step 2, step 3. Starting at 0 or 1 is a design choice — keep the condition matched to that choice, just like with for.
let total = 0;
let k = 1;
while (k <= 4) {
total = total + k;
k++;
}
console.log(total);
Accumulate a total
total starts at 0. Each round adds the current k, then advances k. After 1+2+3+4, total is 10. The growing result lives outside the loop; the counter that drives the condition lives beside it.
let left = 3;
while (left > 0) {
console.log("left=" + left);
left--;
}
console.log("done");
Count down to a stop
Not every loop only goes up. This one prints left=3, left=2, left=1, then done. Decreasing the counter is still an honest update — anything that moves toward ending the loop is fine.
let x = 0;
do {
console.log("tick " + x);
x++;
} while (x < 1);
console.log("after=" + x);
do...while runs the body first
do...while executes the body once, then checks the condition. Even if the condition would already be false for a normal while, the first round still happens. Here you see tick 0, then after=1. Use it when “try once, then maybe repeat” matches the story.
Show while results on the page
Run a short count with while, build a message, then write it into the page.
<!DOCTYPE html>
<html>
<head>
<title>While — page count</title>
</head>
<body>
<p id="out">…</p>
<script>
let msg = "";
let i = 1;
while (i <= 4) {
msg = msg + i + " ";
i++;
}
let box = document.getElementById("out");
box.textContent = "count=" + msg.trim();
</script>
</body>
</html>
The page shows count=1 2 3 4. Change the upper bound, refresh, and read the new list. Prefer for when the three-part plan is obvious up front; prefer while when the stop rule is clearer as a single condition with updates in the body. Next lesson walks values with for...of and for...in.
Try it yourself
- Write a
whilethat logs0…4withi < 5andi++in the body. - Change it to log
1…5withi <= 5. - Sum
1…5intototalwithwhileand log the result. - Count down from
5to1, then logdone. - Write a
do...whilethat prints one line even when the condition starts false. - In the HTML demo, change only the upper bound, refresh, and read
count=.
Common mistakes
- Forgetting the body update so the condition never becomes false (infinite loop)
- Off-by-one:
i < 5vsi <= 5when you meant the other count - Choosing
do...whilewhen you truly want zero rounds possible - Putting the update in the wrong place or changing a different variable than the condition reads
- Using a long
whilewhen a clearforheader would be easier to read
Summary
whilerepeats a block while its condition stays true- Prepare variables before the loop; update them inside the body
- Great for counting, summing, and counting down
do...whileruns the body at least once, then checks- Prefer
forwhen start/stop/step are obvious; preferwhilefor a single ongoing rule - Next: for...of and for...in — walking values and keys
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top