Search code examples
javascriptjsonobject

How to delete full object with javascript?


I tried it, but delete doesn't work.

var x = {"y":42,"z":{"a":1,"b":[1,2,3]}};

alert(x.y);
delete x;

alert(x.y); // still shows '42'

How to delete the full object crossbrowser?

Edit: x = null also doesn't work


Solution

  • You can only use the delete operator to delete variables declared implicitly, but not those declared with var. You can set x = null or x = undefined