You will be able to use a JavaScript while loop to repeat a block of code until a condition becomes false, while updating the condition safely to avoid infinite loops.
What it is
A while loop is a control-flow statement that checks a condition before each repetition. If the condition is true, JavaScript runs the loop body. If it is false, the loop stops and execution continues after the loop. The mental model is: “keep doing this as long as this remains true.” Related terms include loop body, condition, counter, sentinel value, and infinite loop.
Unlike a for loop, a while loop does not require the initialization, condition, and update to be written in one line. This makes it useful when the number of repetitions is unknown at the start.
Why it matters
- It lets you process data until a stopping point, such as reading items from an array or list.
- It supports waiting for a state change, such as polling until a value is found.
- It helps implement game loops, retry logic, and input validation.
- It makes code shorter than copying the same statements many times.
- It teaches careful condition design, which is important for avoiding infinite loops.
Syntax or steps
The smallest useful pattern has three parts: create a variable, test it, and change it inside the loop.
let counter = 0;
while (counter < limit) {
// loop body
counter++;
}
Steps: initialize counter before the loop; write a condition that can become false; run the body; update the variable that affects the condition. If the condition never changes, the loop may never end.
Example
This example prints numbers from 0 to 2 and then stops.
let i = 0;
while (i < 3) {
console.log(i);
i++;
}
Part by part:
let i = 0;creates a counter starting at zero.while (i < 3)checks whetheriis still less than three.console.log(i);prints the current value.i++;increasesiby one, moving the loop toward its stopping condition.
The loop runs three times: when i is 0, 1, and 2. When i becomes 3, the condition is false, so the loop exits.
Common mistakes
- Forgetting to update the condition variable. If
inever changes,i < 3stays true. Fix: include an update such asi++or change the value being tested. - Starting with a condition that is already false. The loop body may never run. Fix: check the initial value and decide whether that behavior is intended.
- Using the wrong comparison.
i <= 3runs one extra time compared withi < 3. Fix: count the expected number of repetitions. - Putting the update outside the loop. If the update is after the closing brace, it runs only once. Fix: place the update inside the loop body.
When to use it
Use a while loop when you do not know how many repetitions are needed, or when the stopping condition depends on changing data. Use a for loop when the number of repetitions is known and you want initialization, condition, and update grouped together.
| Loop | Best when | Example condition |
|---|---|---|
while | Repeating until a state changes | while (found === false) |
for | Repeating a known number of times | for (let i = 0; i < 3; i++) |
Practice
Guided exercise: write a while loop that prints the numbers 1 through 5.
let n = 1;
while (n <= 5) {
console.log(n);
n++;
}
Expected output: 1 2 3 4 5, each on its own line.
Challenge: use a while loop to count down from 3 to 1. Hint: start at 3, test n > 0, and use n--.
Quick check
Question: What must be true for a while loop to stop?
Answer: Its condition must eventually become false, usually because the loop body changes a variable used in the condition.
Summary
A while loop repeats code as long as a condition is true. The key transferable idea is to design a clear starting state, a stopping condition, and an update that moves the loop toward that condition.