Home  >  Article  >  Web Front-end  >  The difference between adding var and not adding var when defining variables in javascript_Basic knowledge

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

WBOY
WBOYOriginal
2016-05-16 16:25:211449browse

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.

Statement:
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