JS Variable declaration Promotion

Source: Internet
Author: User

1. Variable Promotion

Depending on how JavaScript operates and JavaScript does not have a block-level scope, it can be concluded that the variable declares that the elevation is moved to the top of the scope scope (global domain or current function scope).

    • Variable declaration promoted to global domain
// undefined var a = "Hi", equivalent to var//  variable promoted to global scope //  declared variable A, but not initialized, Solid for undefineda= "Hi";
    • Variable declaration promoted to current function field
 varName = "Boy"; (function () {            if(typeofname = = ' undefined ')            {            varname = ' Girl '; Console.log (' Hi, ' +name); }            Else{Console.log (' Hi, ' +name); }        })();//Console Output Hi,girlequivalentvarName = "Boy"; (function () {            varName//variable promoted to current function scope            if(typeofname = = ' undefined ') {Name= ' Girl '; Console.log (' Hi, ' +name); }            Else{Console.log (' Hi, ' +name);    }        })(); 
2, undefined and undefined (not defined) differences
    • Undefined

  undefinedThere are three ways to express the meaning:

    1. This variable exists , but does not give any value.
    2. A common data type and a value. You can manually assign any variable to undefined, at which point it has no special meaning.
    3. A value in an existing object that does not exist (not declared) is considered to be undefined.

To summarize these three kinds of cases, undefined can be summarized as follows:

    • It is a data type and a value.
    • When this value is present, either it is assigned to itself, or it exists, or the object in which it exists. A completely non-existent variable will not be undefined.
    • Undefined (not defined)

A 未定义 variable (not defined) is a variable that does not have any declarations at all . Such a variable will throw a fatal error directly when used. However, if you use TypeOf to judge such a variable, it will not be error-prone and return undefined , which makes it impossible to use TypeOf to differentiate between the two cases.

JS Variable declaration Promotion

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.