The difference between adding var when defining variables in javascript and without var is that using criptvar
1. The external is global, and the internal is local variables.
2. Add var as a local variable (in the method) without var as a global variable (once used in the method)
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var golbe = "global ";
Test ();
Function test (){
Var local = "local ";
Document. write (golbe );
Document. write (local );
}
Document. write (golbe );
Document. write (local );
</Script>
In the preceding test method, after the var of the local variable is removed, the local variable becomes a global variable. However, if the local variable is not used locally, the local variable as a global variable is invalid.
To verify this, I commented out the code that only uses the local variable in the test method and found that the Code cannot be printed externally.
Summary: global variables do not have to declare var in the var function. Adding or not adding the var keyword when defining global variables does not have any impact; however, if the var keyword is not added when defining a local variable, the javascript interpreter will interpret it as a global variable.