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 returns true even if property does not exist.

// example of deleting a property
var oo = {"k1":1, "k2":2};
delete oo["k1"];
console.log(oo);                // { k2: 2 }
"use strict"

// delete returns true even if property does not exist

var 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

See also: JS: Reflect.deleteProperty

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

JS Object Property Topic

  1. JS: Property Overview
  2. JS: Property Key
  3. JS: Property Dot Notation / Bracket Notation
  4. JS: Create/Delete Property
  5. JS: Get Property, Set Property
  6. JS: Check Property Existence
  7. JS: Access Property, List Properties
  8. JS: Property Attributes
  9. JS: Getter/Setter Properties
  10. JS: Property Descriptor
Like what you read? Buy JavaScript in Depth