A while loop checks its condition before every iteration, so its body may never run. A do...while loop runs its body first and checks the condition afterwards, so the body always executes at least once. That single difference decides which loop to use.
Both loops repeat code until a condition becomes falsy. They work the same way in browsers and Node.js, and the syntax has not changed in modern JavaScript.
The condition of do...while sits after the body, and the loop ends with a semicolon. Automatic semicolon insertion makes it optional, but writing it is standard style.
If the condition is false from the start, the body is skipped completely.
The condition was false from the start, yet the body ran once. This is the behaviour you want when the first pass must happen before you can decide whether to repeat, for example reading input or retrying an operation.
| Aspect | while | do...while |
|---|---|---|
| Condition checked | Before each iteration | After each iteration |
| Minimum executions | 0 | 1 |
| Typical use | Loop while data remains or a state holds | Run once, then repeat if needed |
| Trailing semicolon | None | Conventional after the condition |
| Control keywords | break, continue | break, continue |
A loop whose condition never becomes falsy runs forever and freezes the tab or process. Make sure something inside the body changes the condition.
In do...while, continue jumps to the condition check, not to the top of the body. That detail surprises people who expect it to restart the body directly.
The number of items is not known up front, and the queue may be empty, so while is the natural fit.
The operation must be attempted at least once, and only a failed attempt triggers another.
Always cap the retries, as shown with attempt < 5. Without a cap, a permanent failure loops forever.
Any do...while can be written as a while by running the body once before the loop, but that duplicates code. Any while can be written as a do...while by wrapping the body in an if, which only adds noise. Pick the form that matches the intent.
Use a classic for loop when you have a counter, and for...of to walk an array or any iterable. Use while when the number of iterations is unknown in advance, such as consuming a queue (while (queue.length > 0)). Use do...while only when the body must run once first. In practice it is rare, so reach for it deliberately.
For asynchronous work, await works inside both loops in an async function, which gives you a clean polling or retry pattern. Do not use await in a loop when the calls are independent and could use Promise.all.
while tests the condition before running the body, so it may run zero times. do...while tests afterwards, so it always runs at least once.
Automatic semicolon insertion adds it for you, so the code works without it. Style guides still recommend writing it.
There is no meaningful performance difference. Choose by behaviour, not speed.
Yes. break leaves the loop, and continue moves to the next condition check.
Use while when the loop may need to run zero times, and do...while when the body must execute once before the first check. Always make sure the loop changes the state its condition depends on, and use break or continue for early exits.