1. The division standard of JavaScript scope is function function blocks, not divided by if, while, or for
<script>function f1(){ alert("before for scope:"+i); //i is not assigned (not without declaration! Using undeclared variables or functions will cause fatal errors and interrupt script execution) //At this time the i value is undefined for(var i=0; i<3;i++){ alert("in for scope:"+i);} //The value of i is 0, 1,2 alert("after for scope:"+1); //The value of i is 3, which is already outside the for scope, but the value of i is still retained as 3 while(true){ var j=1; break;} alert(j); //The value of j is 1, which is already outside the while scope, but the value of j is still retained as 1 if(true){ var j=1; break;} alert(j); //The value of j is 1, which is already outside the while scope, but the value of j is still retained as 1 if(true){ var k=1; } alert(k); //The value of k is 1, and it is already outside the scope of if scope, but the value of k is still retained as 1}f1(); //At this time, the function is called outside the function block, and the ijk variable alert(i) existing in the function scope of f1 is output again; //error!!! The reason is that i here is not declared (not unassigned, different from the output of the first line of f1), the script is wrong, and the program ends! alert(j); //Not executed alert(k); //Not executed</script>2. Before JavaScript is executed, it will precompile the entire script file (analyze the declaration part of the script file, including the local variable part), thereby determining the scope of the real variable. For example below:
<script> var x=1; function f2(){ alert(x); //The value of x is undefined! This x is not a global variable, because a local variable with a duplicate name has been declared in the function scope, so the parameter a of the global variable is overwritten. This shows that JavaScript will be precompiled before execution, and x in the function body is pointed to local variables, not global variables. At this time, x is only declared and has no assignment, so it is undefined x=3; alert(x); //The value of x is 3. But the local variable var x is still declared alert(x); //The value of local variable x is declared alert(x); //The value of x is 3 } f2(); alert(x); //The value of x is 1, not within the function scope, the value of x is the value of the global variable. </script>3. When the global variable is duplicated with the local variable, the scope of the local variable will overwrite the scope of the global variable. After leaving the scope of the local variable, it will return to the scope of the global variable. When the global variable encounters the local variable,
How to use global variables? Use window.globalVariableName.
<script> var a=1; function f3(){ alert(window.a); //a bit 1, here a is the global variable var a=2; alert(a); } f3(); alert(a);</script>The above article briefly discusses the global and local variables of JavaScript is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.