for statementS2C Home « Statements « for

Creates a loop structure with three optional expressions.

Description

Creates a loop through a section of code which will be executed n number of times.

Syntax

Signature Description
for ([initialization]; [condition]; [expression])
   statement
Creates a loop structure with three optional expressions.

Parameters

Parameter Description
initializationAn optional (assignment) expression or variable declaration.
  • New variables can be expressed using the var statement.
  • Scope is the same as that of the for statement, not the loop itself.
conditionAn optional expression evaluated before each pass through the loop.
expressionAn optional expression evaluated at the end of each pass through the loop.
statementExecuted when expression evaluates to true.

Examples

The code below shows some for loops.


// execute the loop until loop condition is false.
for (var i=0; i<3; i++) {
  alert('The i variable is = ' + i);
}
alert('left first for loop');

// execute the loop until loop condition is false.
for (var i=10; i>9; i--) {
  alert('The i variable is = ' + i);
}
alert('left second for loop');

Press the button below to action the above code:


Related Tutorials

JavaScript Intermediate Tutorials - Lesson 5 - For Loops

go to home page Homepage go to top of page Top