Summary of JavaScript variables and function promotion

Source: Internet
Author: User

What is a JavaScript variable promotion?

--When the JS program runs,

(a) The declaration of a variable is "lifted" by the interpreter to the top of the body of the method, and the initialization assignment operation is not promoted sequentially

(b) An undeclared variable in the function body that declares a variable outside the body of the function and becomes a global variable

(c) The declared function, the entire function realizes that the interpreter is promoted to the top of the method body, the initialization assignment operation is executed sequentially

1-1 variable Promotion

Eg: the declaration of the variable is lifted, and the initialization assignment is not raised.

<Script>Console.log (a); //undefined    vara=3; //if there is no VAR statement, an error is definedConsole.log (a); //3    functionfn () {console.log (a); //undefined      vara= 'a'; //This is a local variable,without this sentence the result of the upper and lower output is changed to 3Console.log (a); //a} fn ();    Console.log (a); //3</Script>

The above code is quite as follows:

<Body> <Script>    varA;    Console.log (a); //undefineda=3; //if there is no VAR statement, an error is definedConsole.log (a); //3    functionfn () {varA;  Console.log (a); //undefineda= 'a'; //without this sentence the result of the upper and lower output is changed to 3Console.log (a); //a} fn ();  Console.log (a); //3  </Script></Body>

Compare the Var declaration of a in a function

<Script>Console.log (a); //undefined    vara=3; //if there is no VAR statement, an error is definedConsole.log (a); //3    functionfn () {console.log (a); //Become a3a= 'a'; //This a becomes a global variable,without this sentence the result of the upper and lower output is changed to 3Console.log (a);//a} fn ();  Console.log (a); //Become aa</Script>

The above code is quite as follows:

 <Script>Console.log (a); //undefined    vara=3; //if there is no VAR statement, an error is definedConsole.log (a); //3    varA; functionfn () {console.log (a); //3a= 'a'; //without this sentence the result of the upper and lower output is changed to 3Console.log (a); //a} fn ();    Console.log (a); //a</Script>

PS: So in order to avoid calling the function, it is possible to modify the external variables, the variables inside the function are declared initialized, normalized.

1-2 function Promotion

Eg: the declarative function promotes the entire function body to the top, and the literal function only promotes the declared variables.

<Script>Console.log (F1); //function F1 () {}Console.log (F2); //undefined    functionF1 () {}varF2= function () {}; //if there is no Var declaration, the above result error F2 is not definedConsole.log (F2); //function () {}</Script>

The above code is quite as follows:

<Script>    functionF1 () {}//Advance    varF2; //AdvanceConsole.log (F1); //function F1 () {}Console.log (F2); //undefinedF2= function () {}; //if there is no Var declaration, the above result error F2 is not definedConsole.log (F2); //function () {}</Script>

PS: In order to avoid some unexpected variable promotion errors, declare and initialize the variables before starting the scope of each variable, notice that the variables that are not declared in the function body automatically become global variables, resulting in a variety of situations

Summary of JavaScript variables and function promotion

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.