When we look at the JS declaration variable, we often find that there are some variables before the Var but then not, then what is the difference?
If this happens in a function , the variable that is defined by VAR is a local variable, which is defined as a global variable without var.
// using Var var y= "dsh"function Test () { var y= "WX";} test (); Console.log ( y);
[Web browser] "DSH"
// do not use Var var y= "dsh"function Test () { y= "wx";} test (); Console.log (y) ;
[Web browser] "WX"
Under global scope, variables defined with var cannot be delete, and variables defined without var can be delete.
This means that the implied global variable is strictly not a real variable, but rather a property of the global object, because the property can be deleted by the delete and the variable is not available.
var x= "Dsh"; Y= "wx"; Delete x; Delete Y;console.log (x); Console.log (y);
[Web browser] "DSH"
[Web browser] "Uncaught referenceerror:y is not defined"
Using var to define variables also promotes variable declarations, but variables without var do not
Console.log (x); // the lifting variable x is undefinedconsole.log (y); // not promoted, y is not defined. Y is not definedvar x= "Dsh"; Y= "wx";
[Web browser] "Undefined"
[Web browser] "Uncaught referenceerror:y is not defined"
In ES5 ' use strict ' mode, if the variable is not defined with VAR, it will be an error.
2. variable var keyword