Join my Laravel for REST API's course on Udemy 👀

Remove key and value from a JavaScript object

April 30, 2022  ‐ 1 min read

Setting an object property value to undefined is sometimes not enough, when you rely somewhere on the result of Object.keys() for example.

In such a case you want to remove the key and value from the object all together. For this you may use the JavaScript delete operator.

Let's say you have the following object:

const ingredients = {
  basil: true,
  salmon: true,
  tomato: false,
};

In order to delete the tomato property of the ingredients object you may use the following piece of code:

delete ingredients.tomato;

This results in the following object:

console.log(ingredients);
// { basil: true, salmon: true }

In case you have a multi-word property key you should reference the property a little different, using the square braces syntax:

delete object["my property"];