Favorites

JavaScript do...while Statement

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
do...while
JavaScript IterationThis statement is supported by all modern browsers.
Updated 28th November 2026

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

let attempts = 0;
let roll;
do {
  roll = Math.ceil(Math.random() * 6);
  attempts++;
} while (roll !== 6);
console.log('Rolled a 6 after', attempts, 'attempts');

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.

This statement is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee