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.
let result = "";
let i = 0;
do {
i += 1;
result += i;
} while (i < 5);
console.log(result);
// Expected output: "12345"
do statement while (condition);
statementA 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:
break stops statement execution and goes to the first statement after the loop.continue stops statement execution and re-evaluates condition.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.
let result = "";
let i = 0;
do {
i += 1;
result += `${i} `;
} while (i > 0 && i < 5);
// Despite i === 0 this will still loop as it starts off without the test
console.log(result);
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.
do {
if (!user.loggedIn) {
console.log("You are not logged in");
break;
}
const friends = user.getFriends();
if (!friends.length) {
console.log("No friends found");
break;
}
for (const friend of friends) {
handleFriend(friend);
}
} while (false);
// The rest of code
In JavaScript, there are some alternatives, such as using a labeled block statement with break:
handleFriends: {
if (!user.loggedIn) {
console.log("You are not logged in");
break handleFriends;
}
const friends = user.getFriends();
if (!friends.length) {
console.log("No friends found");
break handleFriends;
}
for (const friend of friends) {
handleFriend(friend);
}
}
Or using a function:
function handleFriends() {
if (!user.loggedIn) {
console.log("You are not logged in");
return;
}
const friends = user.getFriends();
if (!friends.length) {
console.log("No friends found");
return;
}
for (const friend of friends) {
handleFriend(friend);
}
}
In some cases, it can make sense to use an assignment as a condition, such as this:
do {
// …
} while ((match = regexp.exec(str)));
But when you do, there are readability tradeoffs. The while documentation has a Using an assignment as a condition section with our recommendations.
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
do...while |
1 | 12 | 1 | 4 | 1 | 18 | 4 | 10.1 | 1 | 1.0 | 4.4 | 1 | 1.0.0 | 1.0 | 0.10.0 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while