1

It seems in JavaScript you can’t delete function arguments but you can delete global variables from a function.

Why this behavior?

var y = 1;
(function (x) { return delete y; })(1); // true

(function (x) { return delete x; })(1); // false
Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
antonjs
  • 14,060
  • 14
  • 65
  • 91
  • 1
    Both return `false` in normal use (i.e. not within the Firebug or browser console, which use `eval()`). See my answer. – Tim Down Aug 10 '11 at 13:14

2 Answers2

6

Actually, neither should return true, and indeed they don't in Firefox or Chrome (untested in other browsers). I imagine you tested this with Firebug or another browser console, which changes things due to the console using eval(). delete only deletes properties of an object and cannot normally delete a variable declared using var, whatever the scope.

Here's an excellent article by Kangax on the subject: http://perfectionkills.com/understanding-delete/

Tim Down
  • 318,141
  • 75
  • 454
  • 536
4

Edit: Both return false in normal use (i.e. not within the Firebug or browser console, which use eval()). See Tim Down’s answer (it should be the accepted one).

Community
  • 1
  • 1
Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
  • 2
    Actually the first example doesn't work because `y` is declared using `var` and therefore has the `[[DontDelete]]` attribute (in ECMAScript 3: the equivalent attribute is `[[Configurable]]` is ECMAScript 5) set to true. – Tim Down Aug 10 '11 at 11:34