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]

// example of using delete operator
const jj = { kk: 1 };
delete jj.kk;
console.log(jj.hasOwnProperty("kk") === false);
// Example of not a object
console.log((delete 3) === true);
// example of property does not exist
const jj = {};
console.log((delete jj.k) === true);
// example of failure due to frozen object
const jj = { p: 1 };

// console.log((delete jj.p) === false);
// error: Uncaught TypeError: Cannot delete property 'p' of #<Object>

delete obj.key
similar to delete obj[key]. [see Property Dot Notation vs Bracket Notation]

Tip: The delete operator never goes up the Prototype Chain.

Tip: Never Use the Delete Operator

JavaScript in Depth

Delete Property