Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The do...while statement creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.
A statement that is executed at least once and re-executed as long as the condition evaluates to true. You can use a block statement to execute multiple statements.
conditionAn expression evaluated after each pass through the loop. If this condition evaluates to true, statement is re-executed. When condition evaluates to false, execution continues with the statement after the do...while loop.
Like other looping statements, you can use control flow statements inside statement:
The do...while statement syntax requires a semicolon at the end, but the automatic semicolon insertion process may insert one for you if the lack of a semicolon results in invalid syntax.
In the following example, the do...while loop iterates at least once and reiterates until i is no longer less than 5.
Because the statement is always executed once, do...while (false) is the same as executing the statement itself. This is a common idiom in C-like languages, which allows you to use break to break out of branching logic early.
In JavaScript, there are some alternatives, such as using a labeled block statement with break:
Or using a function:
In some cases, it can make sense to use an assignment as a condition, such as this:
But when you do, there are readability tradeoffs. The while documentation has a Using an assignment as a condition section with our recommendations.
| ECMAScript® 2027 Language Specification # sec-do-while-statement |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 8, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.