132

Possible Duplicates:
How can I unset a JavaScript variable?
How do I remove a property from a JavaScript object?

I'm looking for a way to remove/unset the properties of a JavaScript object, so they'll no longer come up if I loop through the object doing for (var i in myObject). How can this be done?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali
  • 261,656
  • 265
  • 575
  • 769
  • 3
    Did you make any attempt to research for yourself? When I googled *"javascript delete object properties"*, this was the first result: [How to remove a property from a javascript object](http://stackoverflow.com/questions/208105/how-to-remove-a-property-from-a-javascript-object) – user113716 Jun 26 '11 at 16:29
  • How about respecting the rules of the site? [From the faq:](http://stackoverflow.com/faq) *"Please look around to see if your question has already been asked (and maybe even answered!) before you ask."* – user113716 Jun 26 '11 at 18:13

2 Answers2

134

Simply use delete, but be aware that you should read fully what the effects are of using this:

 delete object.index; //true
 object.index; //undefined

But if I was to use like so:

var x = 1; //1
delete x; //false
x; //1

But if you do wish to delete variables in the global namespace, you can use its global object such as window, or using this in the outermost scope, i.e.,

var a = 'b';
delete a; //false
delete window.a; //true
delete this.a; //true

Understanding delete

Another fact is that using delete on an array will not remove the index, but only set the value to undefined, meaning in certain control structures such as for loops, you will still iterate over that entity. When it comes to arrays you should use splice which is a prototype of the array object.

Example Array:

var myCars = new Array();
myCars[0] = "Saab";
myCars[1] = "Volvo";
myCars[2] = "BMW";

If I was to do:

delete myCars[1];

the resulting array would be:

["Saab", undefined, "BMW"]

But using splice like

myCars.splice(1,1);

would result in:

["Saab", "BMW"]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • 5
    tl;dr can we delete windows["myVar"] or not? – mplungjan Jun 26 '11 at 16:35
  • 2
    the simple answer is yes you can, you cna do: `var x = 1;` and then `delete window.x`; – RobertPitt Jun 26 '11 at 17:13
  • @Robert - thanks. I thought so. Never needed to delete anything ever – mplungjan Jun 26 '11 at 17:31
  • So if I did: `delete foo[bar]`, and then I looped through `foo` via `for (var i in foo) {}` I'll no longer encounter `bar` during that loop? – Ali Jun 26 '11 at 17:38
  • as long as your using the delete on an object `{}` and not an array `[]`, if you are using an array then you would do `Data.splice(index,1)` to remove it completely – RobertPitt Jun 26 '11 at 19:33
  • Updated my answer to inform you of this. – RobertPitt Jun 26 '11 at 19:38
  • 3
    *"you can use it's global object such as window, or using this in the outermost scope i.e"* and *"the simple answer is yes you can, you cna do: var x = 1; and then delete window.x; "* No, you can't. If a global is declared with `var`, it cannot be deleted, not even with `delete window.varname`. Example: http://jsbin.com/ojuyig Whereas if you *didn't* use `var` (if you just create the global by assigning to it, e.g., `window.varname = 42`), you can delete that. But `var` creates an *environment binding* on the global object (`window`, in browsers), and those cannot be deleted. – T.J. Crowder Aug 03 '12 at 16:07
  • 2
    @RobertPitt, delete has no effect on varibles created by "var", so your 3rd example is not correct. see [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete] – Peihui Apr 04 '15 at 17:32
  • 1
    A "bit" late, but in case somebody else finds this answer: I found RobertPitt's statement that "delete" on an array element doesn't remove the index to be wrong. delete *does* remove the index -- it doesn't show up in "for-in" nor in "index-in-array" anymore. The array has a "hole" at that index afterwards. Actually, *splice* is the one that doesn't delete the index because it will re-number subsequent elements to fill the hole (unless you're splicing away the last elements of the array). – Martin Geisse Jan 03 '16 at 11:58
  • There so much incorrect statements in this answer that makes me dizzy... – ramazan polat Oct 30 '17 at 00:32
16

To blank it:

myObject["myVar"]=null;

To remove it:

delete myObject["myVar"]

as you can see in duplicate answers

mplungjan
  • 169,008
  • 28
  • 173
  • 236