Do While Loop
Learn how the do while loop works and when to use it over a regular while loop.
Do While Loop
The do...while loop is a close relative of the while loop — with one important difference.
A while loop checks the condition before running the code. If the condition is false from the start, the code never runs at all.
A do...while loop checks the condition after running the code. This means the code block always runs at least once — no matter what.
// while — may never run
while (condition) {
// runs only if condition is true from the start
}
// do...while — always runs at least once
do {
// runs first, then checks condition
} while (condition);That one difference is what makes do...while the right tool in specific situations.
Basic Syntax
do {
// code to run
} while (condition);Notice the semicolon at the end — while (condition);. It is easy to forget and unlike most things in JavaScript, this one actually matters here.
How It Works
The code runs first. Then the condition is checked. If true — go back and run again. If false — exit. The block is guaranteed to execute at least once because the condition check comes after it.
Basic Example
let count = 1;
do {
console.log(count);
count++;
} while (count <= 5);
// 1
// 2
// 3
// 4
// 5This looks similar to a while loop example — and for straightforward counting it behaves the same. The real difference shows up when the condition starts out false.
The Key Difference — Runs At Least Once
let count = 10;
// while — condition is false from start, never runs
while (count <= 5) {
console.log("while:", count);
count++;
}
// (nothing prints)
// do...while — runs once before checking
do {
console.log("do...while:", count);
count++;
} while (count <= 5);
// do...while: 10count starts at 10. The condition count <= 5 is immediately false. The while loop prints nothing. The do...while loop still prints 10 because it runs the block first, then checks.
When to Use do...while
The best use case is when you need to do something at least once before deciding whether to repeat it.
Showing a menu to a user
let choice;
do {
choice = prompt("Choose an option:\n1. New Game\n2. Load Game\n3. Settings\n4. Quit");
} while (choice !== "4");
console.log("Goodbye!");You always need to show the menu at least once — there is no point checking anything before the user has even seen it. The do...while loop is a natural fit here. Show first, then decide whether to show again.
Input validation
let age;
do {
age = Number(prompt("Enter your age:"));
if (age <= 0 || isNaN(age)) {
console.log("Please enter a valid age.");
}
} while (age <= 0 || isNaN(age));
console.log(`Your age is ${age}.`);You have to ask for input before you can validate it. The do...while loop makes this natural — ask first, check after, repeat if invalid.
Comparing All Three Loops
// for — you know the count
for (let i = 0; i < 3; i++) {
console.log("for:", i);
}
// while — check first, may never run
let i = 0;
while (i < 3) {
console.log("while:", i);
i++;
}
// do...while — run first, check after, always runs at least once
let j = 0;
do {
console.log("do...while:", j);
j++;
} while (j < 3);All three print the same output here — 0, 1, 2. The difference only matters at the edges:
for | while | do...while | |
|---|---|---|---|
| When is condition checked? | Before each iteration | Before each iteration | After each iteration |
| Minimum runs | 0 | 0 | 1 |
| Best for | Fixed count | Unknown count | Must run at least once |
A Real Example — Retry Logic
Imagine an app trying to connect to a server. It should try at least once, and keep retrying up to 3 times if it fails.
let attempts = 0;
let connected = false;
do {
attempts++;
console.log(`Connection attempt ${attempts}...`);
// simulating a successful connection on the 3rd try
if (attempts === 3) {
connected = true;
}
} while (!connected && attempts < 5);
if (connected) {
console.log("Connected successfully!");
} else {
console.log("Could not connect. Try again later.");
}
// Connection attempt 1...
// Connection attempt 2...
// Connection attempt 3...
// Connected successfully!You always attempt at least once. If it fails you try again — but no more than 5 times. This pattern shows up in real applications constantly — API retries, reconnection logic, form resubmission.
In real applications you would not use a synchronous loop for network requests — that would freeze everything. But the logic pattern here is identical to what you would use with async code.
Summary
do...whileruns the code block first, then checks the condition- The block always executes at least once — even if the condition is false from the start
- Use it when you need to run something before you can even check a condition
- Input prompts, menu displays, and retry logic are classic
do...whileuse cases - Don't forget the semicolon at the end —
while (condition);