Home >Web Front-end >JS Tutorial >The difference between adding var and not adding var when defining variables in javascript_Basic knowledge
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)
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.