Variable promotion and function promotion in JS

Source: Internet
Author: User

1 There are only two scopes in JS

A: Global scope

B: Function scope

Before ES6, JS is not a block-level scope.

First to explain what is not block-level scope?

So this is the value of the output variable A that can be printed.

2: What is a variable promotion?

In our JS, code execution Time Two step, 1, Parse 2, step by step execution

Then the variable promotion is the variable declaration is promoted to the top of the scope, that is, whether the variable is declared in the scope of the area, it will be promoted to the top of the scope.

So the above notation is actually equivalent to the following:

See a few examples:

Make a slight change to the example above:

The result will be very different,

Let's look at an example:

3: What is function elevation?

The result of the output is:

Note: A function declaration promotes the declaration and definition of a function to the top of the scope.

If this is the case: functions declared by Function expressions

Example:

The final summary:

1: All declarations will be promoted to the top of the scope.

2: The same variable is declared only once, and the others are ignored.

3: The function declaration takes precedence over the precedence of the variable statement, and the function declaration is promoted along with the part of the function definition.

Variable promotion and function promotion in JS

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.