


The difference between whether JavaScript uses var to define variables, with examples
Jul 28, 2018 pm 01:51 PMAlthough 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.
(1) Look at the example below
<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
(2) Remove var
<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
(3 ) Reason analysis
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.
(1) Look at the example below
<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
(2) Remove var
<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
(3 ) Reason analysis
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial

8 Stunning jQuery Page Layout Plugins
