2

Let's suppose we are in the global scope:

When I declare a variable in JS:

a = 1

which I know is not the proper way (but it is not the question). I can use:

delete a (> returns true)

But when I declare:

var b = 1

I can't use:

delete b (> returns false)

Can anyone explain that behaviour?

MarAja
  • 1,547
  • 4
  • 20
  • 36
  • What's the full code? One is a global variable (without var) while the other is local to the function it is declared in. – slebetman Jan 20 '16 at 09:31

1 Answers1

8

delete is only effective on an object's properties. It has no effect on variable or function names.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

a becomes/is ultimately interpreted as window.a, while b clearly refers to a local variable.

deceze
  • 510,633
  • 85
  • 743
  • 889