Delete operator in JavaScript

One of the lesser known operators in JavaScript is the 'delete' operator. I came to know about this operator when I was searching for a way to delete an element of an associative array.

The Mozilla Reference Docs have this to say about the delete operator...

The delete operator deletes an object, an object's property, or an element at a specified index in an array.

Usage

var arr = {
	"number": 42,
	"year"	: 2007,
	"hello"	: "world",
	"foo"	: "bar"
}
for(var ele in arr) {
	alert(ele + " : " + arr[ele]);
}
delete arr['hello']; //Removes the 'hello' key of the array

for(var ele in arr) {
	alert(ele + " : " + arr[ele]); //The 'hello' key and its value will be missing
}

Compactability

I tested it in Firefox 2.0 and IE 6 - it works in both cases. If you test it in other browsers, post the results in the comment section.

Comments

Kumar S at 29 Jan, 2007 08:53
It works fine in IE7

great.
Reply to this.
Michal Till at 20 Apr, 2007 04:47
Isn't it like the arr['hello'] will be rather set to undefined? AFAIK a variable in JS can be defined and set to undefined value.
Reply to this.
Binny V A at 20 Apr, 2007 10:11
But the index will still come up in for in iterations.
Reply to this.
Anonymous at 05 Jun, 2009 01:23
No it is gone.
Reply to this.
Anonymous at 28 Mar, 2010 08:22
A) That is NOT an array. That is an object. (Well, an array is also, technically, an object... but it's an object of the array class)
You're merely referencing via string (a.k.a. what people who can't program do)

B) Javascript never really deletes anything. Instead, it sets the value to undefined.
In javascript, all namespaces are filled with undefined so it's IMPOSSIBLE to actually delete a namespace. What you can do, however, is delete the values stored in said namespaces.

I'm looking into it, but its purpose should be to guarantee the destruction of an object after you're finished using it. (As far as I'm aware, the values stored in a particular namespace (inclusive of heirachy) are never removed unless the value is explicitly change.

Thus, we delete values that will persist after the code is executed that we do not need around the next pass through.

C)
var a = new Array(5);
a.splice(3,1);

Is the way you remove elements from a REAL array


D) Don't be a Noob, learn how to program before you learn how to script.
Reply to this.
cds at 01 Apr, 2010 10:02
Wow, Anonymous at 28 Mar, 2010 08:22. Never heard anyone say that people who use hashtables don't know how to program. Do you really think it's most efficient to store absolutely everything in an integer indexed array? Google time complexity.
Reply to this.
Anonymous at 21 Apr, 2008 10:43
hi;
I want to delete a particular row when i am clicking a delete button that row should get deleted.and the same page containing remaining data should be uploaded.The existing page is containing thye actions for modifications.
when i am deleting the data from the existing page it is directed to the modification page.
please suggest an answer to this

thank u!!!!!
Reply to this.
BenH at 07 May, 2008 02:16 Reply to this.
nut_t02 at 06 Aug, 2008 09:27
It works fine in FF3 (3.0.1)

^^
Reply to this.
vgebrev at 20 Aug, 2008 02:19
hey, this also works in Adobe JS for PDF, if anyone deals with that, JS is generally more popular for html scripting :)
Reply to this.
John Nicely at 03 Mar, 2009 05:26
Although it's very important to note that you weren't actually working with an associative array. It's an object, because JavaScript doesn't have associative arrays, strictly speaking.
Reply to this.
Anonymous at 13 May, 2009 01:08
Would have been nice to learn this BEFORE I tried writing a linked list class.
Reply to this.
Anonymous at 02 Jul, 2009 09:05
Would have been nice to learn this BEFORE it was relevant.
Reply to this.
Anonymous at 09 Sep, 2009 01:45
>> Compactability
;-)
Reply to this.
Anonymous at 11 Sep, 2009 06:46
working in Gecko/20090729 Firefox/3.5.2
working in Prism 1.05
Reply to this.
Anonymous at 13 Sep, 2009 10:40
Works in IE 8, FF 3.5, Chrome 2, Netscape 8, Opera 9, Safari 4, Seamonkey 1. I don't see why it shouldn't work on any browser that can handle JavaScript 1.2 (which is when it was implemented) or better.
Reply to this.
Anonymous at 13 Sep, 2009 10:42
Been using this since js1.2 and never had any problems (that I know of).
Reply to this.
tester at 10 Dec, 2009 07:14
With Array it is better to use splice method (http://w3schools.com/jsref/jsref_splice.asp)
Reply to this.
Anonymous at 15 Jan, 2010 03:13
And for "normal" objects:

var myObject = { a:["a","b"], b:"x"}
alert(myObject.a)
delete myObject.a
alert(myObject.a)

Reply to this.
Anonymous at 04 Feb, 2010 11:59
NOTE if you delete an element of an array, and then say array.length it will not reflect the new length of the array!!!!!!! (IE7 at least)
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.