for Statements

for loops repeat until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop.

for loop syntax:

for ([initialExpression]; [condition]; 
[incrementExpression]) {
   statements
}

When a for loop executes, the following occurs:

  1. The initializing expression initialExpression, if any, is executed. This expression usually initializes one or more loop counters, but the syntax accepts expressions of any degree of complexity.

  2. The condition expression is evaluated. If the value of condition is true, the loop statements execute. If the value of condition is false, the for loop terminates.

  3. The statements execute.

  4. The update expression incrementExpression executes and control returns to step 2.