JavaScript do...while Statement
The do...while statement runs a block of code once, then keeps repeating it while a condition is truthy.
Description
The do...while statement runs its body at least once, then checks the condition after each iteration and repeats while it's truthy. It's useful when the first pass must happen regardless, such as asking for input until it's valid.
Because the condition is checked at the end, a do...while loop always runs its body at least one time, even if the condition is false from the start. That makes it the natural choice for retry logic, generating random values until one meets a requirement, or menus that are shown once and then repeated until the user chooses to quit. Note the semicolon after the closing parenthesis.
break and continue work as in other loops; continue jumps straight to the condition check. In practice, do...while is the least used of JavaScript's loops, and the same logic can always be written with a while loop and a flag, but it's the most direct way to express "do this, then repeat while...".
Syntax
do {
statements
} while (condition);Example
Browser Support
The following information will show you the current browser support for the do...while statement. Hover over a browser icon to see the version that first introduced support for this statement.
Desktop





Tablets & Mobile





