Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1122299 of delete operator

  • Revision slug: Web/JavaScript/Reference/Operators/delete
  • Revision title: delete operator
  • Revision id: 1122299
  • Created:
  • Creator: Mayankgupta688
  • Is current revision? No
  • Comment

Revision Content

{{jsSidebar("Operators")}}

The delete operator removes a property from an object.

Syntax

 

delete expression 

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

delete object.property
delete object['property']

Parameters

object
The name of an object, or an expression evaluating to an object.
property
The property to delete.

Return value

Throws SyntaxError in strict mode if the property is an own non-configurable property (returns false in non-strict). Returns true in all other cases.

 

Description

 

Unlike what common belief suggests, the delete operator has nothing to do with directly freeing memory .

Memory Managment is done indirectly via breaking references. See the memory management page for more details.

delete operator shall remove the property from the Object. On succesful deletion of property it will return "true" else "false" would be returned.

However it is important to consider following Scenerios:

  1. Even if the Object property which you are trying to delete does not exists, delete keyword shall not do anything and would return "true"
  2. If the property with the same name exists on the Object's Prototype chain, Object shall be using that property from the Prototype Chain.
  3. Any Property declared using "var" keyword cannot be deleted from Global or Function Scope.
  4. Delete Keyword cannot delete the "functions" assigned to Global Scope. It wont work on either Function Definition or Expression in case of Global Scope Functions.
  5. Functions which are part of Object apart from Global Scope (window) can be deleted.
  6. Cannot remove Properties of Predefined Objects like Math, Array, Object. These properties are marked as non-configurable. 
  7. Any property marked as Non-Configurable, cannot be deleted
  8. Does not delete local Variables

We shall be evaluating these scenerios, but lets first see how basic synatax works

var Employee = {
  age: 28,
  name: 'abc',
  designation: 'developer'
}

console.log(delete Employee.name)       //returns true
console.log(delete Employee.age)        //returns true

//If User tries to delete the property that does not exists, "true" is returned 
console.log(delete Employee.salary)     //returns true

 

Marking Property as Non-Configurable

 

We can mark the property of an Object as Non-Configurable using following Code.

Marking any property as Non-Configurable shall not allow delete keyword to delete that property. In case of Non Configurable Properties, false will be returned.

When we talk about Strict Mode, if we try to delete any Non Configurable, property, it will result in Error.

Dont put delete on Non Configurable Properties.

var Employee = {};

Object.defineProperty(Employee, 'name', {configurable: false})

console.log(delete Employee.name);    // return false

 

When we create a variable using "var" keyword, it is marked as Non Configurable. Therefore they cannot be deleted.

var name = 'XYZ';

//We can access properties of Object using:
Object.getOwnPropertyDescriptor(window, 'demoName')   

//output: Object {value: "", writable: true, enumerable: true, configurable: false}
//Since Property is added using "var" keyword, it is marked as "Non Configurable"

delete name;     // return false

In the strict Mode, this will return an Error. In strict Mode we cannot delete Non Configurable Properties.

 

Delete Keyword in "Strict and Non Strict Mode"

 

When a delete opperator is encountered in a "Strict Mode", any direct reference to variable, function argument or function name shall throw "Syntax Error".

In the code below, we can see a direct reference of delete keyword to a variable in a non strict mode. Lets evaluate how it behaves:

Since any variable defined using "var" keyword is marked as "Non Configurable", therefore "salary" property cannot be deleted. Delete keyword will return "false" in this case.

function Employee() { 
  delete salary;
  var salary;
}

Employee()

Lets see how the same code behaves in "Strict Mode". 

Since in the below code we are referencing to the "variable" directly, it would return a "Syntax error".

"use strict";
function Employee() {
  delete salary;         // Syntax Error
  var salary;        
}

// Similarly, access to function directly with "delete" keyword shall return "Syntax Error".

function DemoFunction() {
  //some code
}

delete DemoFunction      // Syntax Error


 

Examples

 

// creates the property adminName on the Global Scope
adminName = 'xyz';            

// creates the property empCount on the Global Scope
// Since we are using "var", property is marked as "Non Configurable"
var empCount = 43;

EmployeeDetails = {
  name: 'xyz,
  age: 5,
  designation: 'Developer'
};


// adminName is a property of the Global Scope
// Can be deleted since it is created without "var" keyword. Therefor Configurable.
delete adminName;       // returns true

// empCount is not configurable, since "var" keyword is used, marking it as Non Configurable          
delete empCount;       // returns false 

// delete can remove Properties from Objects  
delete EmployeeDetails.name; // returns true 

// Even when the property does not exists, it returns "true"
delete EmployeeDetails.salary; // returns true 

// delete doesn't affect certain predefined properties
delete Math.PI; // returns false 

// EmployeeDetails is a property of the Global Scope.
// Since it defined without "var", it is marked Configurable
delete EmployeeDetails;   // returns true

function f() {
  var z = 44;

  // delete doesn't affect local variable names
  delete z;     // returns false
}


 

Lets evaluate what happens if the property exist inside ProtoType Chain.

function Foo(){
  this.bar = 10;
}

Foo.prototype.bar = 42;

var foo = new Foo();

// Return True, since it deletes the property on "foo" Object
delete foo.bar;           

// foo.bar is still available, since it is present in Proitotype Chain.
console.log(foo.bar);

// deletes property on prototype
delete Foo.prototype.bar; 

// logs "undefined", property no longer inherited
console.log(foo.bar);           

 

Deleting array elements

 

When you delete an array element, the array length is not affected. This holds even if you delete the last element of the array.

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.

var trees = ["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[3] is assigned the value undefined, but the array element still exists:

var trees = ["redwood","bay","cedar","oak","maple"];
trees[3] = undefined;
if (3 in trees) {
    // this gets executed
}

 

Specifications

 

Specification Status Comment
{{SpecName('ESDraft', '#sec-delete-operator', 'The delete Operator')}} {{Spec2('ESDraft')}}  
{{SpecName('ES6', '#sec-delete-operator', 'The delete Operator')}} {{Spec2('ES6')}}  
{{SpecName('ES5.1', '#sec-11.4.1', 'The delete Operator')}} {{Spec2('ES5.1')}}  
{{SpecName('ES1', '#sec-11.4.1', 'The delete Operator')}} {{Spec2('ES1')}} Initial definition. Implemented in JavaScript 1.2.

 

Browser compatibility

 

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Temporal dead zone {{CompatUnknown}} {{CompatGeckoDesktop(36)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Temporal dead zone {{CompatUnknown}} {{CompatUnknown}} {{CompatGeckoMobile(36)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}

 

Cross-browser notes

 

Although ECMAScript makes iteration order of objects implementation-dependent, it may appear that all major browsers support an iteration order based on the earliest added property coming first (at least for properties not on the prototype). However, in the case of Internet Explorer, when one uses delete on a property, some confusing behavior results, preventing other browsers from using simple objects like object literals as ordered associative arrays. In Explorer, while the property value is indeed set to undefined, if one later adds back a property with the same name, the property will be iterated in its old position--not at the end of the iteration sequence as one might expect after having deleted the property and then added it back.

So, if you want to simulate an ordered associative array in a cross-browser environment, you are forced to either use two separate arrays (one for the keys and the other for the values), or build an array of single-property objects, etc.

 

See also

 

Revision Source

<div>{{jsSidebar("Operators")}}</div>

<p>The <strong><code>delete</code> operator</strong> removes a property from an object.</p>

<h2 id="Syntax">Syntax</h2>

<p>&nbsp;</p>

<pre class="syntaxbox">
delete <em>expression</em> </pre>

<p>where <em>expression</em> should evaluate to a property reference, e.g.:</p>

<pre class="syntaxbox">
delete <em>object.property</em>
delete <em>object</em>['<em>property</em>']
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code>object</code></dt>
 <dd>The name of an object, or an expression evaluating to an object.</dd>
 <dt><code>property</code></dt>
 <dd>The property to delete.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>Throws <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError">SyntaxError</a>&nbsp;in <a href="/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode">strict mode</a> if the property is an own non-configurable property (returns <code>false</code> in non-strict). Returns <code>true</code> in all other cases.</p>

<h2 id="sect1">&nbsp;</h2>

<h2 id="Description">Description</h2>

<p>&nbsp;</p>

<p>Unlike what common belief suggests, the <code>delete</code> operator has <strong>nothing</strong> to do with directly freeing memory .</p>

<p>Memory Managment is done indirectly via breaking references. See the <a href="/en-US/docs/Web/JavaScript/Memory_Management">memory management</a> page for more details.</p>

<p><strong>delete</strong> operator shall remove the property from the Object. On succesful deletion of property it will return "true" else "false" would be&nbsp;returned.</p>

<p>However it is <strong>important to consider following Scenerios</strong>:</p>

<ol>
 <li>Even if the Object property which you are trying to delete does not exists, <strong>delete </strong>keyword shall not do anything and would return "true"</li>
 <li>If the property with the same name exists on the Object's Prototype chain, Object shall be using that property from the Prototype Chain.</li>
 <li>Any Property declared using "var" keyword cannot be deleted from Global or Function Scope.</li>
 <li>Delete Keyword cannot delete the "functions" assigned to Global Scope. It wont work on either Function Definition or Expression in case of Global Scope Functions.</li>
 <li>Functions which are part of Object apart from Global Scope (window) can&nbsp;be deleted.</li>
 <li>Cannot remove Properties of Predefined Objects like Math, Array, Object. These properties are marked as non-configurable.&nbsp;</li>
 <li>Any property marked as Non-Configurable, cannot be deleted</li>
 <li>Does not delete local Variables</li>
</ol>

<p>We shall be evaluating these scenerios, but lets first see how basic synatax works</p>

<pre>
var Employee = {
&nbsp; age: 28,
  name: 'abc',
  designation: 'developer'
}

console.log(delete Employee.name)       //returns true
console.log(delete Employee.age)        //returns true

//If User tries to delete the property that does not exists, "true" is returned 
console.log(delete Employee.salary)     //returns true

</pre>

<p>&nbsp;</p>

<h3><strong>Marking Property as Non-Configurable</strong></h3>

<p>&nbsp;</p>

<p>We can mark the property of an Object as Non-Configurable using following Code.</p>

<p>Marking any property as Non-Configurable shall not allow <strong>delete</strong> keyword to delete that property. In case of Non Configurable Properties, false will be returned.</p>

<p>When we talk about Strict Mode, if we try to delete any Non Configurable, property, it will result in Error.</p>

<p>Dont put delete on Non Configurable Properties.</p>

<pre>
var Employee = {};

Object.defineProperty(Employee, 'name', {configurable: false})

console.log(delete Employee.name);    // return false
</pre>

<p>&nbsp;</p>

<p>When we <strong>create a variable using "var" keyword,</strong> it is <strong>marked as Non Configurable.</strong> Therefore they cannot be deleted.</p>

<pre>
var name = 'XYZ';

//We can access properties of Object using:
Object.getOwnPropertyDescriptor(window, 'demoName')   

//output: Object {value: "", writable: true, enumerable: true, <strong>configurable: false</strong>}
//Since Property is added using "var" keyword, it is marked as "Non Configurable"

delete name;     // return false</pre>

<p>In the strict Mode, this will return an Error. In strict Mode we cannot delete Non Configurable Properties.</p>

<p>&nbsp;</p>

<h3 id="Temporal_dead_zone"><strong>Delete Keyword in "Strict and Non Strict Mode"</strong></h3>

<p>&nbsp;</p>

<p>When a <strong>delete </strong>opperator is encountered in a "<strong>Strict Mode</strong>", any direct reference to variable, function argument or function name shall throw <strong>"Syntax Error".</strong></p>

<p>In the code below, we can see a direct reference of delete keyword to a variable in a non strict mode. Lets evaluate how it behaves:</p>

<p>Since any variable defined using "var" keyword is marked as "Non Configurable", therefore "salary" property cannot be deleted. Delete keyword will return "false" in this case.</p>

<pre class="brush: js">
function Employee() { 
  delete salary;
  var salary;
}

Employee()
</pre>

<p id="sect2">Lets see how the same code behaves in "Strict Mode".&nbsp;</p>

<p>Since in the below code we are referencing to the "variable" directly, it would return a "Syntax error".</p>

<pre>
"use strict";
function Employee() {
&nbsp; delete salary;         // Syntax Error
&nbsp; var salary;        
}

// Similarly, access to function directly with "delete" keyword shall return "Syntax Error".

function DemoFunction() {
  //some code
}

delete DemoFunction      // Syntax Error


</pre>

<h2>&nbsp;</h2>

<h2 id="Examples">Examples</h2>

<p>&nbsp;</p>

<pre class="brush: js">
// creates the property adminName on the Global Scope
adminName = 'xyz';            

// creates the property empCount on the Global Scope
// Since we are using "var", property is marked as "Non Configurable"
var empCount = 43;

EmployeeDetails = {
  name: 'xyz,
  age: 5,
&nbsp; designation: 'Developer'
};


// adminName is a property of the Global Scope
// Can be deleted since it is created without "var" keyword. Therefor Configurable.
delete adminName;       // returns true

// empCount is not configurable, since "var" keyword is used, marking it as Non Configurable          
delete empCount;       // returns false 

// <strong>delete</strong> can remove Properties from Objects  
delete EmployeeDetails.name; // returns true 

<strong>// </strong>Even when the property does not exists, it returns "true"
delete EmployeeDetails.salary; // returns true 

// <strong>delete</strong> doesn't affect certain predefined properties
delete Math.PI; // returns false 

// EmployeeDetails is a property of the Global Scope.
// Since it defined without "var", it is marked Configurable
delete EmployeeDetails;   // returns true

function f() {
  var z = 44;

  // delete doesn't affect local variable names
  delete z;     // returns false
}


</pre>

<p>&nbsp;</p>

<p>Lets evaluate what happens if the property exist inside ProtoType Chain.</p>

<pre class="brush: js">
function Foo(){
  this.bar = 10;
}

Foo.prototype.bar = 42;

var foo = new Foo();

// Return True, since it deletes the property on "foo" Object
delete foo.bar;           

// foo.bar is still available, since it is present in Proitotype Chain.
console.log(foo.bar);

// deletes property on prototype
delete Foo.prototype.bar; 

// logs "undefined", property no longer inherited
console.log(foo.bar);           </pre>

<h3>&nbsp;</h3>

<h3 id="Deleting_array_elements"><strong>Deleting array elements</strong></h3>

<p>&nbsp;</p>

<p>When you delete an array element, the array length is not affected. This holds even if you delete the last element of the array.</p>

<p>When the <code>delete</code> operator removes an array element, that element is no longer in the array. In the following example, <code>trees[3]</code> is removed with <code>delete</code>.</p>

<pre class="brush: js">
var trees = ["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, <code>trees[3]</code> is assigned the value undefined, but the array element still exists:</p>

<pre class="brush: js">
var trees = ["redwood","bay","cedar","oak","maple"];
trees[3] = undefined;
if (3 in trees) {
    // this gets executed
}</pre>

<h2 id="sect3">&nbsp;</h2>

<h2 id="Specifications">Specifications</h2>

<p>&nbsp;</p>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-delete-operator', 'The delete Operator')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-delete-operator', 'The delete Operator')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-11.4.1', 'The delete Operator')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES1', '#sec-11.4.1', 'The delete Operator')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.2.</td>
  </tr>
 </tbody>
</table>

<h2 id="sect4">&nbsp;</h2>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>&nbsp;</p>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Temporal dead zone</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop(36)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
  <tr>
   <td>Temporal dead zone</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoMobile(36)}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="sect5">&nbsp;</h2>

<h2 id="Cross-browser_notes">Cross-browser notes</h2>

<p>&nbsp;</p>

<p>Although ECMAScript makes iteration order of objects implementation-dependent, it may appear that all major browsers support an iteration order based on the earliest added property coming first (at least for properties not on the prototype). However, in the case of Internet Explorer, when one uses <code>delete</code> on a property, some confusing behavior results, preventing other browsers from using simple objects like object literals as ordered associative arrays. In Explorer, while the property <em>value</em> is indeed set to undefined, if one later adds back a property with the same name, the property will be iterated in its <em>old</em> position--not at the end of the iteration sequence as one might expect after having deleted the property and then added it back.</p>

<p>So, if you want to simulate an ordered associative array in a cross-browser environment, you are forced to either use two separate arrays (one for the keys and the other for the values), or build an array of single-property objects, etc.</p>

<h2 id="sect6">&nbsp;</h2>

<h2 id="See_also">See also</h2>

<p>&nbsp;</p>

<ul>
 <li><a href="https://perfectionkills.com/understanding-delete/">In depth analysis on delete</a></li>
 <li>{{jsxref("Reflect.deleteProperty()")}}</li>
</ul>
Revert to this revision