do...while

Repeats a block of code at least once, then while a condition remains truthy.

Since ES3 Spec ↗

Syntax

do {
  // ...
} while (condition);

Examples

let i = 0;
do {
  console.log(i);
  i++;
} while (i < 3);
Output
0
1
2
// Body runs once even though the condition is false
let n = 10;
do {
  console.log("ran");
} while (n < 5);
Output
ran
let total = 0;
let k = 1;
do {
  total += k;
  k++;
} while (k <= 4);
console.log(total);
Output
10

Notes

- The condition is evaluated after the body, so the body always runs at least once. - Remember the trailing semicolon after `while (condition)`.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also