Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:25:21
Original
1451 people have browsed it

1. The external ones are global and the internal ones are local variables.

2. Add var as a local variable (in the method), and do not add var as a global variable (when used once in the method)

Copy code The code is as follows:


In the above test method, when the var of the local variable is removed, local becomes a global variable. However, if local is not used locally, then this local is invalid as a global variable.

To verify this, I commented out the only code that uses local variables inside the test method. I found that it could not be printed externally.

Summary: Global variables do not need to declare var. Variables within functions must declare var. It has no effect whether to add the var keyword when defining global variables; but if the var keyword is not added when defining local variables, the JavaScript interpreter will It is interpreted as a global variable.

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!