For loop syntax:
For ( statement 1; statement 2; Statement 3)
{
code block to be executed
}
Execute starts before the start of statement 1 (code block). Optional, can be default. You can initialize any (or more) values here, as well as omit statement 1 (such as when a value has been set before the loop starts).
Statement 2 defines the conditions for running loops (code blocks). Optional, can be default. If statement 2 returns True, the loop starts again, and if False is returned, the loop ends. If statement 2 is omitted, then a break must be provided within the loop. Otherwise, the loop cannot be stopped.
Statement 3 executes after a loop (code block) has been executed. Optional, default (e.g. when there is code inside the loop).
Example:
<!DOCTYPE HTML><HTML><Body><Script>Cars=["BMW","Volvo","Saab","Ford"]; for (varI=0; I<Cars.length;i++) {document.write (Cars[i]+ "<br>");}</Script></Body></HTML>
When I=0, I<4, enters the loop, at this time prints "BMW", then I++,i=1;
When I=1, i<4, enter the cycle, at this time print "Volvo", and then i++,i=2;
When i=2, I<4, enters the loop, at this time prints "Saab", then i++,i=3;
When I=3, I<4, enters the cycle, at this time prints "Ford", then i++,i=4;
When i=4, exit the loop.
The value has been set before the loop starts:
var i=2,len=cars.length; for (; i<len; i+++ "<br>");}
The loop is written inside the For loop:
var i=0,len=cars.length; for (; i<+ "<br>"); I+ +;}
For/in Cycle
The JavaScript for/in statement loops through the properties of the object:
var person={fname: "John", lname: "Doe", Age:25}; for inch Person ) { txt=txt + person[x]; }
JavaScript Basics Learning--for Loops