delete operator

  • Revision slug: JavaScript/Reference/Operators/delete
  • Revision title: delete
  • Revision id: 32648
  • Created:
  • Creator: SpoilSpot
  • Is current revision? No
  • Comment /* Deleting array elements */

Revision Content

Summary

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

Operator
Implemented in: JavaScript 1.2, NES3.0
ECMA Version: ECMA-262

Syntax

delete expression

where expression must evaluate to a property reference, e.g.:

delete variableName
delete objectExpression.property
delete objectExpression["property"]
delete objectExpression[index]
delete property // legal only within a with statement

Parameters

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.

x=42
var y= 43
myobj=new Number()
myobj.h=4      // create property h
delete x       // returns true (can delete if declared implicitly)
delete y       // returns false (cannot delete if declared with var)
delete Math.PI // returns false (cannot delete predefined properties)
delete myobj.h // returns true (can delete user-defined properties)
delete myobj   // returns true (can delete objects)

Deleting array elements

When you delete an array element, the array length is not affected. For example, if you delete a{{mediawiki.external(3)}}, a{{mediawiki.external(4)}} is still a{{mediawiki.external(4)}} and a{{mediawiki.external(3)}} is undefined.

When the delete operator removes an array element, that element is no longer in the array. In the following example, trees{{mediawiki.external(3)}} is removed with delete.

trees=new Array("redwood","bay","cedar","oak","maple")
delete trees[3]
if (3 in trees) {
   // this does not get executed
}

If you want an array element to exist but have an undefined value, use the undefined value instead of the delete operator. In the following example, trees{{mediawiki.external(3)}} is assigned the value undefined, but the array element still exists:

trees=new Array("redwood","bay","cedar","oak","maple")
trees[3]=undefined
if (3 in trees) {
   // this gets executed
}
{{ wiki.languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_sp\u00e9ciaux/L\'op\u00e9rateur_delete", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_specjalne/Operator_delete" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>The delete operator deletes an object, an object's property, or an element at a specified index in an array.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Operator</td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.2, NES3.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval">delete <i>expression</i>
</pre>
<p>where <i>expression</i> must evaluate to a property reference, e.g.:
</p>
<pre class="eval">delete <i>variableName</i>
delete <i>objectExpression.property</i>
delete <i>objectExpression</i>["<i>property</i>"]
delete <i>objectExpression</i>[<i>index</i>]
delete <i>property</i> // legal only within a with statement
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>objectName</code> </dt><dd> The name of an object.  
</dd></dl>
<dl><dt> <code>property</code> </dt><dd> The property to delete.  
</dd></dl>
<dl><dt> <code>index</code> </dt><dd> An integer representing the array index to delete.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>The fourth form is legal only within a <code>with</code> statement, to delete a property from an object.
</p><p>You can use the <code>delete</code> operator to delete variables declared implicitly but not those declared with the <code>var</code> statement.
</p><p>If the <code>delete</code> operator succeeds, it sets the property or element to <code>undefined</code>. The <code>delete</code> operator returns true if the operation is possible; it returns false if the operation is not possible.
</p>
<pre>x=42
var y= 43
myobj=new Number()
myobj.h=4      // create property h
delete x       // returns true (can delete if declared implicitly)
delete y       // returns false (cannot delete if declared with var)
delete Math.PI // returns false (cannot delete predefined properties)
delete myobj.h // returns true (can delete user-defined properties)
delete myobj   // returns true (can delete objects)
</pre>
<h4 name="Deleting_array_elements"> Deleting array elements </h4>
<p>When you delete an array element, the array length is not affected. For example, if you delete a{{mediawiki.external(3)}}, a{{mediawiki.external(4)}} is still a{{mediawiki.external(4)}} and a{{mediawiki.external(3)}} is undefined.
</p><p>When the <code>delete</code> operator removes an array element, that element is no longer in the array. In the following example, trees{{mediawiki.external(3)}} is removed with <code>delete</code>.
</p>
<pre>trees=new Array("redwood","bay","cedar","oak","maple")
delete trees[3]
if (3 in trees) {
   // this does not get executed
}
</pre>
<p>If you want an array element to exist but have an undefined value, use the <code>undefined</code> value instead of the <code>delete</code> operator. In the following example, trees{{mediawiki.external(3)}} is assigned the value undefined, but the array element still exists:
</p>
<pre>trees=new Array("redwood","bay","cedar","oak","maple")
trees[3]=undefined
if (3 in trees) {
   // this gets executed
}
</pre>
<div class="noinclude">
</div>
{{ wiki.languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Op\u00e9rateurs/Op\u00e9rateurs_sp\u00e9ciaux/L\'op\u00e9rateur_delete", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Operatory/Operatory_specjalne/Operator_delete" } ) }}
Revert to this revision