6

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?

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • 1
    MDN says Returns false only if the property exists and cannot be deleted. It returns true in all other cases. After deleting, try testing the actual values. You'll see that `a` was not deleted. – jeremy Sep 14 '12 at 23:31
  • 1
    @Nile: Yeah, the behaviour is different. I don't see how this *explains* anything. – Randomblue Sep 14 '12 at 23:32
  • It behaves differently _because it is defined to_? What else do you want? – Eric Sep 14 '12 at 23:33
  • 2
    @Eric: Maybe an explanation of *why* it was defined to? – Sasha Chedygov Sep 14 '12 at 23:35

4 Answers4

4

From the MDN docs:

The delete operator removes a property from an object.

A global variable (without var) is a property on the global object (typically window), so can be deleted.

A var is not a global variable, but a local variable in the outer scope - not a property of the global object - so delete does not delete it. From those docs:

x = 42;     // creates the property x on the global object
var y = 43; // declares a new variable, y

delete x;   // returns true  (x is a property of the global object and can be deleted)
delete y;   // returns false (delete doesn't affect variable names)
Eric
  • 95,302
  • 53
  • 242
  • 374
  • 1
    Declared and undeclared global variables are both properties of the global object. They only differ based on the value of the `configurable` attribute. For more info, see: http://stackoverflow.com/a/16007360/1306809 – Matt Coughlin Apr 15 '13 at 15:11
3

MDN says delete returns false only if the property exists and cannot be deleted. It returns true in all other cases. After deleting, try testing the actual values. You'll see that a was not deleted. This is because, as the MDN page says, delete will not affect variable names.

It has no effect on variable or function names.

(i.e., defined with var and not off the global object)

Take a look at the examples on the following page. https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/delete

jeremy
  • 9,965
  • 4
  • 39
  • 59
2

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.

Nelson
  • 49,283
  • 8
  • 68
  • 81
0

"var a" means it cannot be accessed from anywhere outside of the current block, thus deleting it WOULD mean UNDECLARE (not same as undefined), thus allowing to write "var a" again in the same block (error).

Allowed usages (MDN):

delete object.property 
delete object['property'] 
delete object[index] 
delete property

It's like GOTO and unstructured programming, where you might need to manually clean up resources, it's kind of ~Destructor in C (though not the same). You can delete an object like ~a(); but you cannot "'UNDECLARE' a variable" like "int i".

Max
  • 1,463
  • 5
  • 19
  • 34