DEV Community

Dhairya Shah
Dhairya Shah

Posted on โ€ข Originally published at codewithsnowbit.hashnode.dev

1 1

Avoid the "delete" keyword in Javascript

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}
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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 ๐Ÿ˜Š

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay