Although javaScript has many similarities with java and c languages, there are also differences.
JavaScript variables do not have block scope. The only ones that have their own scope are functions.
<script type="text/javascript"> //定义全局变量 var scope="全集变量"; function test(){ //全局变量被局部变量覆盖 document.writeln(scope+"<br/>") //定义scope局部变量,起作用范围是整个函数 var scope="局部变量"; //再次输出scope值 document.writeln(scope); } test();</script>
Run result:
undefined
Local variable
<script type="text/javascript"> //定义全局变量 var scope="全集变量"; function test(){ //全局变量被局部变量覆盖 document.writeln(scope+"<br/>") //定义scope局部变量,起作用范围是整个函数 scope="局部变量"; //再次输出scope值 document.writeln(scope); } test();</script>
in test(). Running result:
Full set of variables
Local variables
The main reason is that there are differences between defining variables with var and without var:
①If you use var variables, then the program will forcefully define a new variable
②If If a variable is not defined using var, the system will first search whether the variable exists in the current context. Only if the variable does not exist, the system will redefine a new variable
The reason why the above changes from undefined to "Global variable" is because the code that defines the local variable does not define a new variable, but directly creates a global variable scope and assigns it a value. Therefore, test() does not cover the global variable scope, so it also outputs "global variables"
Although JavaScript has many similarities with Java and C languages, there are also differences.
JavaScript variables do not have block scope. The only ones that have their own scope are functions.
<script type="text/javascript"> //定义全局变量 var scope="全集变量"; function test(){ //全局变量被局部变量覆盖 document.writeln(scope+"<br/>") //定义scope局部变量,起作用范围是整个函数 var scope="局部变量"; //再次输出scope值 document.writeln(scope); } test();</script>
Run result:
undefined
Local variable
<script type="text/javascript"> //定义全局变量 var scope="全集变量"; function test(){ //全局变量被局部变量覆盖 document.writeln(scope+"<br/>") //定义scope局部变量,起作用范围是整个函数 scope="局部变量"; //再次输出scope值 document.writeln(scope); } test();</script>
in test(). Running result:
Full set of variables
Local variables
The main reason is that there are differences between defining variables with var and without var:
①If you use var variables, then the program will forcefully define a new variable
②If If a variable is not defined using var, the system will first search whether the variable exists in the current context. Only if the variable does not exist, the system will redefine a new variable
The reason why the above changes from undefined to "Global variable" is because the code that defines the local variable does not define a new variable, but directly creates a global variable scope and assigns it a value. Therefore, test() does not cover the global variable scope, so the "global variable" is output.
Related articles:
Related videos:
JavaScript Basics Strengthening Video Tutorial
The above is the detailed content of The difference between whether JavaScript uses var to define variables, with examples. For more information, please follow other related articles on the PHP Chinese website!