DEV Community

Sham Gurav
Sham Gurav

Posted on

( JavaScript Tip πŸ’‘ ) - delete Operator

delete operator is lesser-known operator in JavaScript. This operator is more specifically used to delete JavaScript object properties.

The delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.

const User = {
  firstName: 'John',
  lastName: 'Wick'
};
​
delete User.lastName;
​
console.log(User);
// Output: { firstName: "John" }

console.log(User.lastName);
// Output - undefined
Enter fullscreen mode Exit fullscreen mode

If the property which you are trying to delete does not exist, delete will not have any effect and will return true.

If a property with the same name exists on the object's prototype chain, then, after deletion, the object will use the property from the prototype chain (in other words, delete only has an effect on own properties).

Note - delete operator doesn’t delete property value rather the property itself.

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

Most of this is copy pasted verbatim from MDN