The delete operator deletes an object, an object's property, or an element at a specified index in an array.
Syntax
delete objectName
delete objectName.property
delete objectName[index]
delete property // legal only within a with statement
Parameters
Parameter | Description |
---|---|
objectName | The name of an object. |
property | The property to delete. |
index | An integer representing the array index to delete. |
Description
The fourth form is legal only within a with statement, to delete a property from an object.
You can use the delete operator to delete variables declared implicitly but not those declared with the var statement.
If the delete operator succeeds, it sets the property or element to undefined. The delete operator returns true if the operation is possible; it returns false if the operation is not possible.
Deleting array elements
When you delete an array element, the array length is not affected. For example, if you delete a[3], a[4] is still a[4] and a[3] is undefined.
When the delete operator removes an array element, that element is no longer in the array. In the following example, trees[3] is removed with delete.
If you want an array element to exist but have an undefined value, use the undefined keyword instead of the delete operator. In the following example, trees[3] is assigned the value undefined, but the array element still exists: