do-while Loop
do {
//code
} while (condition);
do
,while
are keywordscondition
is a boolean expressioncode
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