JavaScript variable Promotion

Source: Internet
Author: User

Elevation is a mechanism that moves the declaration of variables and functions to the top of the function scope (or global scope if not within any function).

Ascension affects the life cycle of a variable, and the life cycle of a variable consists of 3 phases:

declaration-- create a new variable, such as Var myvalue;

Initialize-- Initializes a variable with a value, such as myvalue=150;

Use-- use the value of the variable, such as alert (myvalue).

JavaScript does not strictly follow this order, so it provides more flexibility. For example, the use of a function can precede a declaration .

This is because the function declarations of JavaScript are promoted to the top of the scope.

Variable promotion has different effects in different ways:

Variable declaration: Use VAR, let, const keyword

function declaration: Use function () {...} Grammar

class declaration: Using the Class keyword

A function declaration creates and initializes a variable within the scope of the function. By default, the value of a variable that is declared but uninitialized is undefined.

1. boost VAR: A variable declared with VAR is promoted to the top of the function scope, and if the variable is accessed before the declaration, its value is undefined. Although its declaration was lifted, its assignment was not promoted to the top of the function scope, and remained in place.

function sum (A, b) {var myString;             // lift to the top Console.log (myString);           // undefinedmystring= "HelloWorld";             // assignment not affected Console.log (mystriing);              // ' HelloWorld ' return A +b;    }

2. Block-level scope variables: let

The Let declaration creates and initializes a variable within a block-level scope: By default, the value of a variable declared but uninitialized is undefined.

Let is a huge improvement introduced by ECMASCRIPT6, which allows code to maintain modularity and encapsulation at the level of code blocks;

Variables defined with let are promoted to the top of the code block. However, if the variable is accessed before the declaration, JavaScript throws an exception Referenceerror:is not defined.

At the top of the code base, the variables are as if they were in a temporary death interval and cannot be accessed.

function Istruthy (value) {var myvariable= ' value 1 '; Console.log (myvariable);       // Value1 if (value) {Console.log (myvariable);     // referenceerror:myvariable is not defined, temporary Death Zone let myvariable= ' Value 2 '; Console.log (myvariable);       //Value 2returntrue;}}

The above code confirms that the let variable is actually promoted.
Let to promote in block-level scope the variable is protected from the outer scope. first declare, then use.

3. Constant const

A constant declaration creates and initializes a constant within a block-level scope: When a variable is declared, the variable must be initialized in the same statement. After declaration and initialization, the value of a variable cannot be modified.

Constants defined with const are promoted to the top of the code block. Because of the temporary death interval, constants cannot be accessed until they are declared. The constant lift effect is the same as the Let declaration variable.

4. Function declaration: Function declaration creates a function with the supplied name and arguments

The promotion of a function declaration allows you to use the function anywhere within the scope of the owning domain, even before the declaration. Functions can be accessed anywhere within the current scope or sub-scope.

Note: function declaration functions () {...} and function Expression var=function () {...} The difference between

5. Class declarations

Class declarations are promoted to the top of the block-level scope. But if you use a class before the declaration, JavaScript throws an exception. Declare the class first, and then create the instance. Similar to let's lifting effect.

JavaScript variable 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.