DEV Community

Sham Gurav
Sham Gurav

Posted on

3 3

( 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

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

πŸ‘‹ Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spiritsβ€”leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay