The odd for loop notation in JS (JavaScript)!

Source: Internet
Author: User

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; "/>

    1. 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; " />

    1. 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; " />

    1. 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; " />

    1. 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)!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.