Skip to main content

Control Structures

Javascript provide a number of mechanisms to run statements in a loop. The simplest of the JavaScript loop constructions is the while() loop, which uses the following syntax:

 
while (expression) {
    statements
}
 
expression A comparison or logical expression (that is, an expression that returns true or false) that determines when JavaScript stops looping.
statements A block of statements to execute each time through the loop.


Here's how this loop works:

  1. Evaluate the expression in the while() statement.
  2. If the expression is true, continue with step 3; if the expression is false, skip to step 5.
  3. Execute each of the statements in the block.
  4. Return to step 1.
  5. Exit the loop (that is, execute the next statement that occurs after the while() block).

Next: For Loop