This problem is caused by the running results of a JS program:
Copy Code code as follows:
var i = 0;
function A () {
for (i=0;i<20;i++) {
}
}
Function B () {
for (i=0;i<3;i++) {
A ();
}
return i;
}
var result = B ();
The operation result of this procedure is results = 21;
As we can see from this program, I have a value of 20 when the A function returns, which is no problem.
When the B function returns, the value of I is 20 or 21 is worth discussing.
The essence of the problem is: the first to Judge I<3, or first i++, and then judge whether the i<3.
According to the result of the execution, it is shown that the i++ is executed first.
Copy Code code as follows:
function A () {
for (i=0;i<20;i++) {
No var i
Here's I is the global variable everyone can access
}
}
Function B () {
for (i=0;i<3;i++) {
alert (i)//empathy Here I am also a global variable, returns 0 and returns only once
A ();//This function returns is I=20
When i=20 after i++ i=21 then does not conform to the i<3 condition, exits directly. So return i=21 this is normal!
}
return i;
}
var result = B ();
Here we complete the order in which the For loop is executed:
Take the following procedure as an example
Copy Code code as follows:
for (int i=0;i<10;i++)
{
}
First Execute i=0;i<10; Then execute the first round of the loop body
Then execute the:i++,i<10; Then execute the second round of the loop body
Until the last i++ I >=10, at this time the loop ends.
That
Statement 1 executes before the Loop (code block) starts
Statement 2 defines conditions for running loops (code blocks)
Statement 3 executes after the Loop (code block) has been executed