Skip to main content

Do while

You can also turn to JavaScript's third loop type, which specializes in executing a block of statements at least once. It's called a do...while() loop, and its general syntax looks like this:

 
do { 
    statements
} 
while (Expression)
 
statements A block of statements to execute each time through the loop.
Expression A comparison or logical expression that determines how many times JavaScript runs through the loop.


This structure ensures that JavaScript executes the loop's statement block at least once.

Here's how JavaScript executes this kind of loop:

  1. Execute each of the statements in the block.
  2. Evaluate the Expression in the while() statement.
  3. If Expression is true, return to step 1; if Expression is false, continue with step 4.
  4. Exit the loop.

Next: Selection using Branching