JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Follow publication

Avoid the “delete” Keyword in JavaScript

--

Hello Folks 👋

What’s up, friends? This is SnowBit here. I am a young, passionate and self-taught front-end web developer with an intention to become a successful one.

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

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 content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

--

--

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Written by Dhairya Shah

I am 16 years old and have an intention to become a successful developer- I usually write about JavaScript and share some tips about it. Happy Coding!

Responses (3)

Write a response