while and do...while

JavaScript ES2024 · ✓ verified by execution on 2026-08-10

A while statement creates a loop that executes a specified block of code as long as a test condition evaluates to true.

This is useful when you don’t know exactly how many times you need to repeat an action, but you know the condition that should stop it.

The while loop

The condition in a while loop is evaluated before executing the statements inside the body.

javascript
let count = 0;
while (count < 3) {
  console.log(count);
  count++;
}
Output
0
1
2

Because the condition is checked first, if the condition is false initially, the loop body is completely skipped:

javascript
let condition = false;
while (condition) {
  console.log('This will never run');
}
console.log('Finished');
Output
Finished

Loops are commonly used to accumulate values over time. In this example, the loop adds the current value of n to x until n reaches 3:

javascript
let n = 0;
let x = 0;
while (n < 3) {
  n++;
  x += n;
}
console.log(x);
Output
6

The do…while loop

Sometimes you need the loop to run at least once, regardless of whether the condition is true or false. That’s where do...while comes in.

A do...while loop executes a specified statement at least once, and then re-executes it as long as the condition evaluates to true. The condition is evaluated after executing the statement.

javascript
let i = 0;
do {
  i += 1;
  console.log(i);
} while (i < 2);
Output
1
2

Notice what happens if the condition is already false when the loop starts. Because the check happens at the end, the body runs once anyway:

javascript
let num = 5;
do {
  console.log(num);
  num++;
} while (num < 5);
Output
5

Comparing the execution flow

while Condition true Code block false do...while Code block Condition true false
Execution flow of while vs do...while

Avoiding infinite loops

Make sure the condition in a loop eventually becomes false—otherwise, the loop will never terminate!

If you do not update the variable that the condition relies on, the program will get stuck running the loop forever, crashing the page or script. Always ensure the loop variable is updated dynamically:

javascript
let val = 10;
while (val > 0) {
  val -= 5;
}
console.log(val);
Output
0

Common Misconceptions

A very common mistake when learning about while loops is assuming the loop body will always run at least once.

Try to predict the outcome below. Does the loop body execute?

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

let runs = 0;
while (runs > 0) {
  runs++;
}
console.log(runs);
Output
0

Edge Cases

Check yourself

What is the main difference between a while loop and a do...while loop?

Reveal answer

A while loop checks its condition before running the body, while a do...while loop checks after — A while loop evaluates the condition first (if false, it never runs). A do...while loop executes the body first, then checks the condition, guaranteeing it runs at least once.

If you omit the curly braces around a while loop body that contains multiple lines, what happens?

Reveal answer

Only the very first line after the while statement is included in the loop body — Without curly braces, only the immediately following statement is considered part of the loop. This often causes infinite loops if the variable update statement gets excluded!

What happens if a while loop condition is given a non-boolean value like a string or a number?

Reveal answer

The loop coerces the value to a boolean based on truthiness — Like if statements, while loops automatically evaluate the truthiness of their condition. Strings or numbers will be coerced to true or false.

Why does a while(true) loop need a break statement inside its body?

Reveal answer

Because without it, the condition never becomes false and the loop runs forever — A loop condition must eventually become false to terminate. If it is hardcoded to true, it is an infinite loop unless explicitly broken out of with a break statement.

Challenges

Challenge 1 +10 XP

Write a while loop that logs numbers from 3 down to 1 (inclusive).

javascript
  • Test 1 — expects "3\n2\n1"
Need a hint? (−25% XP)

Use `while (countdown > 0)` and remember to decrease the countdown inside the loop using `countdown--`.

Show solution (0 XP)
let countdown = 3;
while (countdown > 0) {
  console.log(countdown);
  countdown--;
}

Challenge 2 +15 XP

Write a do...while loop that logs the string 'Ran once' exactly one time, using a condition that is immediately false.

javascript
  • Test 1 — expects "Ran once"
Need a hint? (−25% XP)

Put `console.log('Ran once');` inside the `do` block, and `while (condition);` at the end.

Show solution (0 XP)
let condition = false;
do {
  console.log('Ran once');
} while (condition);