JS: Delete operator

By Xah Lee. Date: . Last updated: .

delete obj[key]

Deletes the property key from obj.

The delete operator never goes up the prototype chain.

delete return true even if property does not exist.

// example of deleting a property
const obj = {"k1":1, "k2":2};
delete obj["k1"];
console.log(obj); // { k2: 2 }
// delete returns true even if property does not exist

const obj = {};
console.log ( delete obj["k"] );
// true

Note, delete expr just return true if expr is not a object.

console.log ( delete 3 ) // true

Note: do not use delete operator to delete array element. That results a sparse array.

[see JS: Sparse Array]

Use Array.prototype.splice to delete elements in array.

[see JS: Array.prototype.splice]

Note, avoid deleting or adding properties to object. Because, due to implementation, adding/deleting properties is inefficient. Instead, change the value to null.


Reflect.deleteProperty instead. It returns 「false」 if delete failed.

[see JS: Reflect.deleteProperty]


ECMAScript 2015 §ECMAScript Language: Expressions#sec-delete-operator

JS Object Property

  1. Property Overview
  2. Property Key
  3. Dot vs Bracket Notation
  4. Create/Delete Property
  5. Get/Set Property
  6. Check Property Existence
  7. Access Property
  8. List Properties
  9. Property Attributes
  10. Getter/Setter
  11. Property Descriptor
  12. Symbol
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog