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.


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


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.

blog comments powered by Disqus