JavaScript » Statements » For

for ([initial-expression]; [condition]; [increment-expression])

The for statement creates a loop consisting of three optional expressions enclosed in brackets and separated by semicolons, and a block of statements to be executed.

The first expression is used to initialise a counter variable, the second (optional) provides a condition that is evaluated on each pass through the loop, and the third updates or increments the counter variable.


for(i=0; i<10; i++)
   document.write(i + ".<BR>");

This example simply counts up from zero for as long as the counter is less than 10.