JavaScript: Delete operator

By Xah Lee. Date: . Last updated: .
delete obj[key]
Deletes the property key from obj.
Return true when:
  • Property exist and is deleted.
  • Property does not exist.
  • Argument is not an object.

Return false when key exist but deletion failed. Such as when the property is not configurable. [see Property Attributes]

delete obj.key
Property Dot Notation vs Bracket Notation

The delete operator never goes up the prototype chain.

const obj = {x:1};
delete obj.x;

console.log( 
obj.hasOwnProperty ("x") === false
); // true

Example: of property does not exist:

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

Example of failure due to frozen object:

const obj = {p: 1};
Object.freeze ( obj );

console.log( 
delete obj.p === false
); // true

console.log( 
obj.hasOwnProperty ("p")
); // true

Example of not a object:

console.log( delete 3 ); // true

Tip

You should NEVER use the delete operator.

βˆ‘ JS in Depth
XAH Β BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc