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