JS for loop, why must add var to define I variable _javascript tips

Source: Internet
Author: User
For example, below.
Copy Code code as follows:

for (i=0;i<10;i++) {//is not written as: Var i=0
alert (i);
}

However, this is really not a good habit, below I will say why write JS for the loop must add Var, or you will occasionally bring annoying difficult to find bugs.
For example, now we want to implement this function: output
10
20
30
40
50
60
70
80
90
100
The following code implementation, Writenumber from 1 to 10 loops, each loop calls the Tentimes method returns 10 times times the index value.
Copy Code code as follows:

<script type= "Text/javascript" >
function Writenumber () {
for (i = 1; I <= i++) {
document.write (Tentimes (i) + "<br/>")
}
}
function Tentimes (v) {
var result = 0;
alert (i);
for (i = 1; I <= i++) {
result = V;
}
return result;
}
Writenumber ();
Alert (i)
</script>

You will find that the output is only 10. You can run the test using the following code box.
<script type= "Text/javascript" > Function Writenumber () {for (i = 1; i <=; i++) {document.write (i ) + "<br>"); } function Tentimes (v) {var result = 0; alert (i); for (i = 1; I <=. i++) {result = V; return result; } writenumber (); Alert (i) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

About adding Var in the Writenumber and Tentimes methods, which means declaring index variable i has 4 kinds of cases:
In the first case, the Writenumber and tentimes each have 1 for loops, and there is no var declaration i index variable in 2 loops.
Run result: Will alert out 1. The result is only 10, not what we want.
Analysis: When performing writenumber, its scope does not find the declared variable I, directly to the I assignment, then implicitly declare I as a global variable, (for variables not declared inside the function, if assigned to it, it is implicitly declared as a global variable.) Loop start, I=1, Tentimes method, found that Tentimes method also did not declare the variable i, so tentimes i is the global variable I, and Writenumber I became the same one. At this time Line9 alert out of the natural is 1. Tentimes Cycle 10 times, so that the global I into 11, natural Writenumber will not perform the 2nd cycle operation.
Validation: If you add alert (i) to the Writenumber () statement, and then uncomment the LINE16, you will find that alert is 12 (12=10+2 i++), which proves that I is a Windows object at this time.
In the second case, Writenumber declares the I variable, that is, the Line3:var I=1,tentimes does not declare the I variable, that is, Line10:i=1.
Run Result: Line9 alert (i) reported I did not define the error, because Writenumber has declared the variable I, so did not become a global i,tentimes execution and did not declare I, so the report is undefined. If you comment out the line9, the output is correct. Because when the tentimes runs to the I=1, the implicit I declaration is a global variable and does not affect the I in Writenumber. Writenumber still performs 10 cycles.
Validation: If you add alert (i) to the Writenumber () statement, and then cancel the Line16 annotation, you will find that alert is in one (11=10+tentimes i++) and that there is windows.i at this time.
In the third case, Writenumber does not declare the I variable, that is, the line3:i=1,tentimes declares the I variable, that is, Line10:var i=1.
Run Result: eject 10 undefined. Because Writenumber does not declare I, implicitly declares I to be a global variable, and Tentimes has declared the variable I (in addition, the declaration of a variable is done in precompilation), so line9 alert (i) I am not windows.i, but Tentimes declared variable I, of course, is undefined now. At the same time, it is found that the output is correct because the tentimes I does not affect the Writenumber global i,writenumber still executes 10 cycles.
Fourth: Writenumber and tentimes all use Var to declare I.
Run Result: Comment off line9, do not say, good habit, the result of course perfect.
Although the second to third situation output is correct, but the use of I is very confusing, it should be lucky to lead to the correct result, because just one is window.i, one is the internal function of the private variable I, so that there is no conflict.
This article is about why it is important to add Var, but it is actually about the scope of the variable (or the life cycle of the variable). After understanding, the following 2 code run results you should be able to accurately say the answer.
<script type= "Text/javascript" > var a = 1; function f () {alert (a); var a = 2; } f (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

<script type= "Text/javascript" > var a = 1; function f () {a = 2; alert (a); } f (); alert (a); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Ps: Speaking of coding's good habits, think of this: if (a==3) should be written as if (3==a). Because we often write = = 1 =, if the variable written on the right only 1 =, will report the compilation error, so you can find errors in time.

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.