Table of Contents
(1) Look at the example below
(2) Remove var
(3 ) Reason analysis
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

Jul 28, 2018 pm 01:51 PM

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

10 jQuery Syntax Highlighters 10 jQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

10 jQuery Syntax Highlighters

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

10  JavaScript & jQuery MVC Tutorials 10 JavaScript & jQuery MVC Tutorials Mar 02, 2025 am 01:16 AM

10 JavaScript & jQuery MVC Tutorials

See all articles