do-while Loop in JavaScript
do-while tests at the bottom: the body always runs at least once, then repeats while the condition holds. That guarantee makes it the prompt loop — show/do first, then ask whether to continue.
The trailing semicolon (} while (...);) is mandatory. Prefer plain while when zero iterations must be possible; reach for do-while exactly when the first pass is unconditional.
Body first, test after
do { ... } while (cond); runs the block, then checks — one execution guaranteed.
Don't forget the semicolon
The closing }; is required and unique among loops.
Prompts are the use case
Do the thing, then test whether to repeat — the action must precede any decision about it.
while vs do-while
Zero-or-more means while; one-or-more means do-while.
let shows = 0;let choice;do { shows++; choice = 9;} while (choice !== 9);console.log("shown", shows);Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.