Ask for advice on function promotion and variable promotion of js

var foo = 3;
function aa(){
    var foo;
    console.log(foo); // undefined ? 3
    foo = foo || 5;
    console.log(foo); //5
}

the problem is shown above,

W3C document-- redeclare the js variable so that its value does not disappear.

var carname="Volvo";
var carname;

so the value of carname does not change,
but why redeclare the value of foo,foo in the function to become undefined instead of 3?


the new scope within the function will give priority to the value of the nearest scope
, so your aa function declares that foo will give priority to foo


in this function. There is a local scope in the

function. Use var to declare foo in the function. Here the foo scope is limited to the function aa , and no longer has a global scope, so it is not equivalent to foo declared by the global scope var .

before ES6, there are only two scopes: global scope and function scope, and a new block-level scope is added after ES6.


function scope, in the aa function var foo, when the execution of this line, the compiler will ask the scope, whether there is a foo variable, if not declared in the scope of the foo variable, the conclusion is, of course, there is no foo variable, so it is undefined. The one in the W3C document, because your variables are all in the same scope, so when you execute the var carname; line, the compiler will ask the scope if the variable carname already exists, and the conclusion is, of course, it does. If you put var foo = 3 in front of, var foo in aa


use "var" in the same scope, when you define the same variable multiple times, the compiler will use the value assigned by the last definition. However, when there is no initial value in the subsequent definition, the compiler ignores the definition of the field this time. When a variable in a parent scope is redefined in a self-scope, the compiler creates a variable with the same name for the current scope and initializes it (the default is undefined), even though the current variable name is the same as the parent scope. When JS executes, the query for variables is based on the nearest principle, because there are defined variables in the current scope, so the variable references in the current function scope will not be looked up, so the variable values defined in your current {} scope will be thrown. The


function looks for variables from the nearest scope first, but cannot find them further up. So when the first statement console.log (foo) looks for the foo variable, look in the function and find that the old value of var foo; is undifined,. If there is no var foo; statement, it will continue to look one level up. At this time, it is 3. Before


ES6, there were two types of scopes: function scope and global scope. If it is declared inside the function, the declared variable inside the function will be given priority; in addition, whether in the function scope or within the global function, if a variable is declared and assigned for the first time, only no value is declared for the second time. The value of the variable is the first time to declare the assigned value.

Menu