Saturday, November 21, 2009

Less know JavaScript operator 'Delete'

One of the lesser known operators in JavaScript is the 'delete' operator. I came to know about while reading John Resig's blog.

What does it do?
The delete operator deletes an object, an object's property, or an element at a specified index in an array.

More about it at Mozilla Reference Docs.

Small Example:
var array = {
"int": 42,
"float": 12.5,
"string": "hello world",
"foo" : "bar"
}
for(var arr in array) {
alert(arr + " : " + array[arr]);
}
delete array['string']; //Removes the 'string' key of the array

for(var arr in array) {
alert(arr + " : " + array[arr]); //The 'string' key & its value will be removed
}