Secrets you don't know about in a For loop in JavaScript

Source: Internet
Author: User

S: The For loop here refers to the language of JS oh ...

The composition structure of the For loop;

The code is as follows Copy Code
Why should var i;//be defined in this? See jquery source
For (i=0 i<10; i++) {
This is the loop block.
}

This is a very ordinary "woman", but do you really know her? The following fields are divided:

Red: Defining blocks
Green: Judgment block
Blue: Calculated block
Pink: Execution Block
PS: The name is I up, ah, not the official website
So what is the order of their execution?
Order of execution for loops
Modify the code as:

The code is as follows Copy Code
Why should var i;//be defined in this? See jquery source
For (I=0,console.log ("Red block:" + i); Console.log ("Green block:" + i),i<3; Console.log ("Blue block:" + i), i++) {
Console.log ("Pink block:" + i)
This is the loop block.
}

The results of the console run are:

However, if the conditions are not tenable:

If the execution block has a break,continue keyword:


See results get the following rules:

Order of execution:
Red-> green-> pink-> Blue
Definition block-> judgment block-> execution Block-> compute block
rule:
Red definition block: Used to define a variable (refers to the identity of a loop, as : i), execute only once, after the last step to the next
Green judgment block: Used to determine whether the condition is set up, if it is set to proceed to the next, if not set up then exit the entire cycle
Pink execution block: Used to execute code, if there is a break do not execute the following code, and do not Inue do not execute the following code and proceed to the next
Blue Compute BLOCK: Used to compute variables such as + +,--
PS: Don't get tripped by ++i, i++ 's pit.
Wonderful for loop

  code is as follows copy code

//definition block content
var i = 0,
    len = 10;//data.length
for (; I <len; i++) {
    console.log (i)
}

//definition, calculated block no content
var i = 0,
    len = ten
F or (; i<len;) {
    console.log (i++); know the For loop execution order is this a vegetable dish?

//Even so ... *_*
var i = 0,
    len = ten
for (;;) {
    console.log (i);
    if (++i >=) {
        break;
   }
}

//feel less than while
var i = 0;
while (I <) {
    console.log (i++);
}

Do you have any sense of a new understanding for a?

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.