Control Flow

Do-While Loop in JavaScript

The while loop executes a block of code while a boolean expression evaluates to true. It checks the boolean expression after each iteration. It terminates as soon as the expression evaluates to false.


Syntax
do { 
    //block of code to be executed
} while(booleanExpression);

Notes

Because the do-while loop evaluates the boolean expression at the end of the iteration, the block of code within the loop is guaranteed to run at least once.

booleanExpression results in either a true or false output. It is created using comparing operators (==, >, =, <=, !=).

There can also be multiple boolean expressions within the parentheses (booleanExpression). The boolean expressions are connected through logical operators (&&, ||, !).


Example
var count = 0; 
do { 
    document.write("We are at " + count); 
    count++; 
} while(count < 10);

See Also
Documentation

do...while - JavaScript | MDN

Add to Slack

< While Loop   |   Try Catch Block >

© 2019 SyntaxDB. All Rights Reserved.