JavaScript: 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 JavaScript: Sparse Array

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

〔►see JavaScript: Array.prototype.splice

See also: JavaScript: Reflect.deleteProperty

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

JS Object Property Topic

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