From mozilla docs about var :
The difference is that a declared variable is a non-configurable property of the global object while an undeclared is configurable.
var a;
--> This is a declared variable, because you are using var
, so it's not configurable.
a = 6;
--> This is an undeclared variable, because you are not using var
, so it's configurable.
both syntaxes above will end up with a var named a
attached as a property of the global object (window
typically) and properties has these attributes:
- Writable. If false, the value of the property can not be changed.
- Configurable. If false, any attempts to delete the property or change its attributes (Writable, Configurable, or Enumerable) will fail.
- Enumerable. If true, the property will be iterated over when a user does for (var prop in obj){} (or similar).
that is extracted from ecmascript5 objects and properties , and as you can read, the configurable
attribute of the variable in question affects whether the variable can or cannot be deleted.