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.

else return false. [see Property Attributes]

πŸ’‘ TIP: The delete operator never goes up the Prototype Chain.

// 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 };
Object.freeze(jj);

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

console.log(jj.hasOwnProperty("p"));
delete obj.key
similar to delete obj[key]. [see Property Dot Notation vs Bracket Notation]

πŸ’‘ TIP: Never Use the Delete Operator

BUY Ξ£JS JavaScript in Depth

Delete Property