Some time ago answered a about the definition of variables using the keyword var or not, summed up a review.
1. Variables defined in the scope of a function are local variables, which are defined as global variables without the definition of var.
Use the VAR definition:
var a = ' Hello World ';
function bb () {
var a = ' Hello Bill ';
Console.log (a);
}
BB () ///' Hello Bill '
console.log (a); ' Hello World '
Do not use VAR definition:
var a = ' Hello World ';
function bb () {
a = ' Hello Bill ';
Console.log (a);
}
BB () ///' Hello Bill '
console.log (a); ' Hello Bill '
2. Under global scope, variables defined with Var cannot be deleted, and variables without var definitions can be deleted. It also shows that the implied global variable is strictly not a real variable, but a property of the global object, because the attribute can be removed by delete, and the variable is not available.
3. Using the var definition variable also promotes the variable declaration, which is
Use the VAR definition:
function hh () {
console.log (a);
var a = ' Hello World ';
}
HH () //undefined
Do not use VAR definition:
function hh () {
console.log (a);
A = ' Hello World ';
}
HH () //' A is not defined '
This is the declaration using the variable defined by Var in advance.
4. In ES5 's ' use strict ' mode, if the variable is not defined with the Var, an error occurs.