while Loops
A while loop repeats code as long as a condition remains true.
Use a while loop when you do not know the exact number of times the loop should run.
Examples:
- Keep asking for input until it is valid.
- Keep rolling a die until you get a specific number.
- Keep reading data until there is no more data.
- Keep retrying until a condition changes.
Basic Structure
A while loop has one condition.
while (condition) {
// code to repeat
}Before every iteration, JavaScript checks the condition.
If the condition is truthy, the block runs.
If the condition is falsy, the loop stops.
Basic Countdown Example
let count = 3;
while (count > 0) {
console.log(count);
count--;
}
console.log("Blastoff!");Output:
3
2
1
Blastoff!Here is what happens:
countstarts at3.- JavaScript checks
count > 0. - If true, it prints
count. count--decreases the value.- The condition is checked again.
- The loop stops when
countbecomes0.
The update step is important:
count--;Without it, the loop would never stop.
while vs for
Use a for loop when the number of iterations is known or naturally index-based.
for (let i = 0; i < 5; i++) {
console.log(i);
}Use a while loop when the number of iterations depends on a condition that may change dynamically.
let attempts = 0;
let success = false;
while (!success) {
attempts++;
success = attempts === 3;
}The difference:
| Loop | Best for |
|---|---|
for |
Known counts, indexes, arrays |
while |
Unknown counts, dynamic conditions |
Rolling Until a Condition Is Met
Here is a common while example: roll a die until you get 6.
let roll = 0;
let attempts = 0;
while (roll !== 6) {
roll = Math.floor(Math.random() * 6) + 1;
attempts++;
console.log(`Rolled a ${roll}`);
}
console.log(`It took ${attempts} attempts to roll a 6.`);You do not know how many times this loop will run.
It might roll a 6 on the first attempt.
It might take many attempts.
That makes while a good fit.
Using while with Input-Like Conditions
In real applications, a while loop can represent repeated checking.
let password = "";
let attempts = 0;
while (password !== "secret" && attempts < 3) {
attempts++;
password = attempts === 3 ? "secret" : "wrong";
}
console.log(`Attempts: ${attempts}`);In a browser, real user input is usually handled with events and forms rather than blocking loops. But the logic is similar:
keep trying while the password is wrong and attempts remaindo...while
do...while is a variation of while.
The condition is checked after the loop body runs.
That means the code runs at least once.
do {
// code to run at least once
} while (condition);Example:
let energy = 0;
do {
console.log("Drinking coffee...");
energy += 10;
} while (energy < 50);This loop runs once before checking energy < 50.
Then it continues while the condition remains true.
while vs do...while
while may run zero times.
let count = 0;
while (count > 0) {
console.log(count);
}The condition is false from the start, so the block never runs.
do...while runs at least once.
let count = 0;
do {
console.log(count);
} while (count > 0);Output:
0Use do...while only when the first run should always happen.
Infinite Loops
An infinite loop happens when the condition never becomes false.
let isActive = true;
while (isActive) {
console.log("Running...");
}This loop never stops because isActive never changes.
A safer version:
let isActive = true;
let runs = 0;
while (isActive) {
console.log("Running...");
runs++;
if (runs === 3) {
isActive = false;
}
}The loop stops after three runs.
Updating State Inside the Loop
Every while loop should have some path that moves it toward stopping.
Counter example:
let count = 0;
while (count < 3) {
console.log(count);
count++;
}Condition-changing example:
let found = false;
let attempts = 0;
while (!found && attempts < 5) {
attempts++;
if (attempts === 4) {
found = true;
}
}The condition changes because attempts changes and found can become true.
Using break with while
Sometimes you want to stop a loop from inside the body.
Use break.
let count = 0;
while (true) {
count++;
if (count === 3) {
break;
}
}
console.log(count); // 3while (true) creates an intentional infinite loop, but break provides the stopping condition.
Use this carefully. Make sure the break condition is reachable.
Common Mistakes
Forgetting to Update the Condition
let count = 0;
while (count < 5) {
console.log(count);
}count never changes, so this loop never stops.
Fix:
let count = 0;
while (count < 5) {
console.log(count);
count++;
}Using while When for Is Clearer
This works:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}But this is clearer for a known count:
for (let i = 0; i < 5; i++) {
console.log(i);
}Use the loop that best communicates your intent.
Writing Conditions That Never Become False
let count = 10;
while (count > 0) {
console.log(count);
count++;
}This moves in the wrong direction. count gets larger, so count > 0 stays true.
Fix:
let count = 10;
while (count > 0) {
console.log(count);
count--;
}Summary
A while loop repeats code while a condition remains truthy.
Remember:
- Use
whilewhen the number of iterations is not known ahead of time. - The condition is checked before each iteration.
- If the condition starts false, a
whileloop may run zero times. - Use
do...whilewhen the block must run at least once. - Always update state so the condition can eventually become false.
- Be careful to avoid infinite loops.
- Use
forwhen the loop is based on a clear counter or array index. - Use
breakcarefully when stopping from inside the loop body.
Next, you will learn about break and continue, two tools for controlling loop execution more precisely.