How to remove properties or methods from an object in JavaScript?

Published June 17, 2020

Properties and methods from an object can be deleted using the delete operator


We know that we can add properties and methods to an object using the . or the [] notation.

To delete, there are not any methods in the Object() constructor function.

We have to use the delete operator.

const John = {
  name: "John Doe",
  age: 23,
  bio: "I'm a human",
};

Let's now remove the bio property from John object.

const John = {
  name: "John Doe",
  age: 23,
  bio: "I'm a human",
};

// delete bio property
delete John.bio;

/*

Result:
--------

{
    name: "John Doe", 
    age: 23, 
}

*/

You have to write the delete keyword followed by the property you want to remove.

This applies to remove methods from an object.

const John = {
  name: "John Doe",
  age: 23,
  bio: "I'm a human",
  sayHello: () => {
    console.log("Hello!");
  },
};

// delete sayHello function
delete John.sayHello;

/*

Result:
--------

{
    name: "John Doe", 
    age: 23, 
    bio: "I'm a human"
}


*/

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter