This article introduces JS development for the JavaScript Intensive tutorial series
Recently, in an interview, I've seen a couple of these for loops. The first reaction is to be ignorant.
After a closer look, there's no problem.
First, the code is a normal loop.
[JavaScript] view plain copy 650) this.width=650; "src=" Https://code.csdn.net/assets/CODE_ico.png "width=" height= "Alt=" on code to see "style=" Border:none; "/> 650) this.width=650;" Src= "https ://code.csdn.net/assets/ico_fork.svg "width=" "height=" "alt=" derived from My Code slice "style=" border:none; "/>
for var i=0;i<10;i++{}
This f-cycle is a normal cycle, no problem at all.
The second Kind
[JavaScript] view plain copy 650) this.width=650; "Src=" https://code.csdn.net/assets/ Code_ico.png "width=" "height=" "alt=" on code to view the chip "style=" Border:none; "/>650" this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "width=" height= "alt=" derived from My Code slice "style=" border:none; " />
-
for var i=10;i--;{}
Cycle result is 10 cycles normal
Third Kind
for (;;);
This would be a dead loop.
Fourth type
[JavaScript] view plain copy 650) this.width=650; "Src=" https://code.csdn.net/assets/ Code_ico.png "width=" "height=" "alt=" on code to view the chip "style=" Border:none; "/>650" this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "width=" height= "alt=" derived from My Code slice "style=" border:none; " />
for (; 1;) {}
[JavaScript] view plain copy 650) this.width=650; "Src=" https://code.csdn.net/assets/ Code_ico.png "width=" "height=" "alt=" on code to view the chip "style=" Border:none; "/>650" this.width=650; "src=" Https://code.csdn.net/assets/ico_fork.svg "width=" height= "alt=" derived from My Code slice "style=" border:none; " />
This is still a dead loop.
Let's think about the structure of the For loop for (expression 1 expression 2 expression 3) {loop-body}
Expression 1 is an initial value
Expression 2 is the condition is true when the loop resumes the next time the loop is stopped
Expression 3 is a variable increment
Also note that the dot JS expression can not be written but "" cannot be omitted.
And then we'll analyze a few special ways.
Notation 2 expression 3 omitted but expression 2 is a bear 10 to 0 descending number so in 10~1 when the expression 2 for the true loop at the time of 0 is a false stop loop.
3 All the conditions are not. That must be a dead loop.
notation 4 Expression 2 constant for the true loop has been going on ~ ~ ~ ~ ~ ~ ~ ~ ~ ~!
This article is from the "11722655" blog, please be sure to keep this source http://11732655.blog.51cto.com/11722655/1791057
The odd for loop notation in JS (JavaScript)!