Home > Web Front-end > JS Tutorial > The difference between whether JavaScript uses var to define variables, with examples

The difference between whether JavaScript uses var to define variables, with examples

php是最好的语言
Release: 2018-07-28 13:51:57
Original
1649 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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:

Analysis of the difference between adding var and without var when defining variables in javascript_javascript skills

The difference between adding var and not adding var when defining variables in javascript_Basic knowledge

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template