do...while
loop in JavaScript works like while
loop in JavaScript the only difference is that do
block executes at least once before while
loop’s test condition. That’s why do block executes whether it satisfies the while condition or not.
Syntax
do { // statements to be executed } while([condition]);
statements: the code statements that are executed at least once and then for each condition evaluated to true.
condition: expression that is to evaluated to decide whether to execute the next iteration of loop. If true, the loop is re-executed otherwise it terminates the loop.
Example
var i=1; do { document.write('<font size=\"' + i + '\">Hello World</font><br />'); i++; } while(i<0);
Above example will display the “Hello World” string once before the while condition i<0
is being tested.