do-while Loop
do {
//code
} while (condition);
do
,while
are keywords.condition
is a boolean expression.code
is any valid JavaScript code.
do-while loop execution starts with the running code inside do block, then condition is evaluated. If condition is true, then code inside do loop is executed, and the condition is evaluated again, and so on. If condition evaluates to false, then do-while loop statement execution is completed.
Examples
1. do-while loop to print 'hello world'
5 times.
let i = 0;
do {
console.log('hello world');
i++;
} while (i < 5);
2. do-while loop when the condition is always false.
let i = 0;
do {
console.log('hello world');
i++;
} while (false);
The code inside do-block executes at least once, even when the condition is always false.