JavaScript » Special » delete

The delete operator is used to delete an object, an object's property or a specified element in an array, returning true if the operation is possible, and false if not.

To delete an object's property, you must precede that property's name with the name of the object, unless it's used in a with statement.


fruit = new Object; = 'apple';
fruit.color = 'green';
fruit.size = 'large';

delete fruit.size;
   delete color;
delete fruit;

With the defined object 'fruit', the delete operations as in the examlple are possible:

fruit = new Array ("apple", "pear", "orange", "cherry", "grape");
delete fruit[2];

The delete operator can also be used to delete an element of an array. This does not affect the length of the array or any of the other elements but changes the deleted element to undefined. This example creates an array called 'fruit' and then deletes element #2 (orange).