The difference between global variables and local variables in JavaScript

Source: Internet
Author: User

Today to see the great God wrote an article, their understanding of global variables and local variables is not deep enough, so write this article, to do a summary.

The Great God Code + understand the text as follows:

Parse: In the above code, the variable i is var declared by the command and is valid at global scope, so there is only one variable globally i . Each cycle, the value of i the variable will change, and within the loop is assigned to the inside of the a function of the array, the console.log(i) inside of the i point is the global i . That is, all of the a members of the array i , pointing to the same i , resulting in the runtime output is the last round of the i value, that is, 10.

First, the definition

Local variables: Variables are declared within a function and can only be accessed inside a function.

Global variables: Variables are defined outside the function and can be called by the entire code.

Declaration keywords: var. In JavaScript, you can use a hermit's variable without declaring the variable directly. The variables declared by a hermit are always used as global variables in JavaScript.

second, the variable promotion (this knowledge point if not understand, then it is easy to pit)

Because the JS engine works by first getting all declared variables executed one at a, the declaration statement of the variable is promoted to the head of the current code block.

Note: Code block/Method block ===> refers to the function of a pair of curly braces {}, avoid, is a function block, and for, while, if, block is not the scope of the demarcation standard.

Third, further understanding

The effect of code promotion so the code that actually executes is this :

<Scripttype= "Text/javascript">    vara= 1; functiontest1 () {varA;//I unassigned "The local variable takes precedence over the global variable when it has the same name as the global variable, so the global variable is overwritten, and a is declared but not defined"alert (a);//so the execution alert value is undefineda= 2;    alert (a);    } test1 (); alert (a);//undefined 2</Script>

Iv. examples

The scope of a JavaScript variable is divided according to the method block (that is, the function's pair of curly braces {}). Here again: It is a function block, and the for, while, if blocks are not scoping criteria. Its variables are not promoted.

The scope of the JS is only the function scope and the global scope, when it is not in the function that is the global scope. "Reference address: http://www.cnblogs.com/zhus/p/6513741.html"

<Scripttype= "Text/javascript">    functiontest2 () {alert ("before for scope:" +i); //I unassigned "is not undeclared! Interrupt script execution with an undeclared variable or function that throws a fatal error         for (varI= 0; I< 3; I++) {alert ("In for scope:" +i); //The value of i is 0, 1, 2, when I jumps out of the Loop 3 o'clock} alert ("After for scope:" +i); //The value of I is 3, note that this is already outside of the for scope, but the value of I still remains at 3         while (true) {            varJ= 1;  Break;    } alert (j); //The value of J is 1, note that it is now outside the while scope, but the value of J still remains at 1        if (true) {            vark= 1;  } alert (k); //the value of K is 1, note that it is now outside the if scope, but the value of K is still reserved at 1} test2 (); //If at this time (outside the function scope) the output only exists in the function scope of the test2, I, J, K variables will be the divine horse effect? alert (i);//error! Yes, it is error because the variable i is not declared (not unassigned, distinguishes the first line of the Test2 function output), resulting in a script error, the program to the end! Alert ("will this line print also output? "); //not executedalert (j);//not executedalert (k);//not executed</Script>

Reference: http://blog.csdn.net/zyz511919766/article/details/7276089

The difference between global variables and local variables in 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.