DEV Community

Cover image for Avoid "delete" keyword in JavaScript to remove property
Hardique Dasore
Hardique Dasore

Posted on

Avoid "delete" keyword in JavaScript to remove property

The delete keyword is used to remove a property from an object in JavaScript. When a property is deleted, it cannot be accessed. The syntax for using the delete keyword to remove a property is as follows:

delete objectName.propertyName;
Enter fullscreen mode Exit fullscreen mode

For example:

const myObject = { a: 1, b: 2 };

// Removing the 'a' property from the object
delete myObject.a;
console.log(myObject);  // Output: { b: 2 }
Enter fullscreen mode Exit fullscreen mode

Using delete on variable will remove variable from memory as well, so be careful and make sure that variable is not being referenced anywhere else before deletion.

Deleting property in JavaScript using destructuring

Using destructuring, you can easily create a new object without the property you want to remove. Destructuring allows you to extract properties from an object and assign them to variables. When you destructure an object, you can specify which properties you want to extract and which properties you want to ignore.

Here's an example of how you can use destructuring to create a new object without a specific property:

const myObject = { a: 1, b: 2, c: 3 };

// Using destructuring to create a new object without the 'b' property
const { b, ...updatedObject } = myObject;
Enter fullscreen mode Exit fullscreen mode

In this example, the properties of myObject that are not b is assigned to updatedObject variable. This b variable will contain the value of b property from myObject. This will not mutate the original object myObject.

You can also use destructuring in a function argument

function myFunction({ a, c }) {
    // The 'b' property is not destructured and is therefore not accessible within this function
    console.log(a,c)
}

myFunction(myObject);
Enter fullscreen mode Exit fullscreen mode

In this way you can filter out the specific properties that you don't want and also you can use destructuring in function argument to only extract the specific properties you want.

Happy Coding!

Top comments (5)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Using delete on variable will remove variable from memory as well, so be careful and make sure that variable is not being referenced anywhere else before deletion.

Using delete on a variable does not do anything - delete is for deleting properties of objects.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

You have not explained why we should avoid using delete? Also, you're comparing apples with oranges... the destructuring method does not 'delete' anything - it merely creates a new object without the property. The whole point of delete is to remove something entirely - freeing up the resources it was using.

Also, what do you mean when you say the property cannot be re-added? Unless I'm misunderstanding, this is 100% wrong

Collapse
 
hardiquedasore profile image
Hardique Dasore

Hi Jon,

I just realized my mistake and have updated the post. Also the developers should avoid the 'delete' keyword when they don't want to mutate the original object.

Thank you.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

If they don't want to mutate the object, then the only choice is to work with a copy of the object - using delete on the original object is not even an option - not something to be avoided. I think the issue here is a poorly worded title. If you are removing a property from an object - you are modifying that object, and delete is how to do that (there is no other way AFAIK)

Your examples show two entirely different things: deleting a property from an object, and creating a copy of an object with a property excluded from the copy - not the same thing at all.

If they do make a copy of the object, then they are also free to use delete on that new object - even if the property being deleted is an object from elsewhere - it's only the property that is deleted. For some, this may be arguably a more readable solution than destructuring (although it will be less efficient if making a new object with multiple properties excluded) - but readability is purely subjective.

Collapse
 
rmaurodev profile image
Ricardo

Great insight!