CKTIS / JavaScript
do — a loop with a post-body condition
The do keyword starts a do...while loop. Its body executes before the first condition check, so the body is entered at least once, even when the condition is false from the outset.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
do {
statements
} while (condition);do is a reserved keyword that starts a do...while statement. It cannot be a variable name. The loop first executes its body, then evaluates the condition in while (condition) and converts its value to a Boolean. If the result is true, it starts another iteration.
Unlike while, which checks the condition before executing the body, do...while enters its body at least once. It suits operations that must precede the decision to repeat. It is a statement, not an expression that produces a value for assignment.
The body may be a single statement; braces {} group multiple statements. Write the trailing semicolon after while (condition);. A continue targeting this loop proceeds to the condition check, whereas break exits it without that check. A return or an exception can also interrupt execution before the condition check.
Ensure the loop can terminate: a permanently true condition without another exit causes endless repetition. In the example, count increases to 1 before the false condition count < limit is checked.
Examples
let count = 0;
const limit = 0;
do {
count += 1;
} while (count < limit);
console.log(count);Result
1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.