For statement syntax:
for (initialization; Expression post-loop-expression) statement
Initialization expression controls expression after loop expression
Example:
var count = ten; for (var i = 0; i < count; i++) { alert (i);}
The above code defines the initial variable of I as 0. The code in the loop body may not be executed only if the conditional expression (I < count) is present. If the code in the loop body is executed, it is bound to evaluate the post-loop expression (i++), which is the value of increment i.
This for Loop statement has the same functionality as the following while statement:
var count = ten; var i = 0; while (I < count) { alert (i); I+ +;}
The For loop does not work with a while loop. In other words, the For loop simply concentrates the loop-related code in one place.
The VAR keyword can also be used in the initialization expression for a for loop. The initialization of the variable can be performed externally, for example:
var count = ten; var i; for (i = 0; i < count; i++) { alert (i);}
The initialization expression, control expression, and post-loop expression in the For statement are optional. Omitting all three expressions will create an infinite loop, for example:
for (; ;) { // infinite loop dosomething ();}
JavaScript for statement