Please consider the following snippet (fiddle here):
var a;
a = 1;
console.log(delete a); // prints 'false'
b = 1;
console.log(delete b); // prints 'true'
Why does the delete keywords behave differently on the global variables a and b?
From the MDN docs:
A global variable (without
var) is a property on the global object (typicallywindow), so can be deleted.A
varis not a global variable, but a local variable in the outer scope – not a property of the global object – sodeletedoes not delete it. From those docs: