JavaScript – Deleting a Property from an Object

The below code shows you how to delete a property from a JavaScript object. Suppose you have the following user object:

var user = {
    "name": "David",
    "age": 18,
    "gender": "Male"
};

And now, you want to delete gender property from this object. To do this, you simply use delete operator. Below are three ways that you can do this.

  • use this way
delete user.gender;
  • or
delete user['gender'];
  • or
var propName = 'gender';
delete user[propName];

0 0 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x