Local variables:
Variables declared within a function, function arguments are local variables
Function nesting:
Each function has its own scope, and local scope nesting occurs
1 var test = "Publice"; // global variable 2 function Fun1 () { 3 var test = "private1"; // 4 function Fun2 () { 5 var test = "privat E2 "; // 6 }; 7 };
View Code
Priority level:
In the function body, local variables are higher than global variables, in other words, local variables in the function body override global variables
Declaration in advance:
JavaScript does not have a block-level scope: The function scope of JavaScript is that all variables declared within a function are always visible within the function, in other words, the var i = 1;,i declared in the If block is a local variable of function and can be accessed within functions.
All variables declared within a function are advanced to the top of the function body, example:
1 var i =20; 2 funtion Test () {3 console.log (i); // output undefined 4 var i =1; 5 if (1==1) {6 i++;} 7 // output 2; 8 };
View Code
Equivalent to:
1 var i =20; 2 funtion Test () {3 console.log (i); // output undefined 4 if (1==1) {5 var i = 1; 6 i++;} 7 Console.log (i); // output 2; 8 };
View Code
JavaScript global variables and local variables