First, let's take a look at 2 pieces of code:
1.
function value () {return 1;} var Value;alert (value);
2.
function value () { return 1;} var value = 1 ; alert (value);
You can think about what the results of 1 and 2 would be.
Let's look at one more topic:
var uname = ' Jack ' function Change () { alert (uname); var uname = ' Lily ' alert (uname) } change ();
The first uname will pop up defined, a lot of people don't understand why. In fact, this is the variable elevation of JavaScript;
Let me first declare 2 variables:
var a = ' a '; var b = ' B ';
In fact, that's how he interprets it:
So the above code will parse into this
var uname = ' Jack ' function change () { var uname; alert (uname); uname = ' Lily '; Alert (uname) } change ();
So we'll know why the first pop-up undefined.
Function declarations are also ahead of the variable declaration. We often see this way of writing:
a (); function A () {}
Or
function A () {}a ();
There is no problem with either of these two formulations. But if the function expression is not, then look down:
var temp function() { console.log (111);}
When you see this, you know the elevation of the variable declaration and the function declaration. function expressions are not promoted.
Well, let's answer it. What was the answer to the first 2 questions? Does everyone know the principle?
JS variable Promotion