Skip to main content

Command Palette

Search for a command to run...

Avoid the "delete" keyword in Javascript

Published
β€’1 min read
Avoid the "delete" keyword in Javascript
D

I am 17 years old and a young passionate and self-taught frontend web developer and have an intention to become a successful developer. I usually write about JavaScript and Web Development and share some tips in the articles.

Hello Folks πŸ‘‹

What's up friends, this is SnowBit here. I am a young, passionate and self-taught frontend web developer and have an intention to become a successful developer.

Today, I am here with an interesting and important topic. So, let's get ready to dive into the topic. Happy Reading!

getting ready


const snowbit = {
    age: 15,
    test: "abc"
}
delete snowbit.test

console.log(snowbit) // {age: 15}

Here, it's better not to use delete to remove the property from the object snowbit.

Let me explain, You should not use delete to remove the property from the object because this mutates the original and that can lead to unpredictable behaviours and becomes difficult to debug.

Instead, use the spread operator to create a new copy.

const snowbit = {
    age: 15,
    test: "abc"
}

const {test, ...newSnowbit} = snowbit

console.log(newSnowbit) //  {age: 15}

Stay tuned for the next article, and make sure to follow if you haven't.


Thank you for reading, have a nice day! Your appreciation is my motivation 😊

More from this blog

D

Dhairya Shah

119 posts