How to understand the global scope and function scope

Introduction: Global variables declared in another scope under other scopes can also be used, but variable declarations under the scope of the function can not be used in the global scope.

var name1 = "Lilei";
function fn() {
    var name2 = "Hanmeimei";
    console.log(name1);
    console.log(name2); 
}

fn(); 
// "Lilei"
// "Hanmeimei"

console.log(name2); // Error: undefined;

 

Note: the ES5 is no block-level scope, or if so for at global scope or {a} declared variables are global variables, or if the if {} for the function or action domain, stated in its interior the variable function may be used in scope;

Guess you like

Origin www.cnblogs.com/aisowe/p/11634879.html