Deleting a property from a JavaScript object.

This is a short tutorial on how to remove a property from a JavaScript object. To do this, we will be using the delete operator.

Take a look at the following JavaScript example:

//An example object that represents a user
var user = {
    name: 'Wayne',
    dob: '1987-11-01',
    email: '[email protected]'
};

//Delete an object property. In this case,
//we will remove the email property from
//our user object.
delete user.email;

//Log the object to the console.
console.log(user);

In the code above, we:

  1. Created a JavaScript object called “user”. This object has three properties: name, dob and email.
  2. We then deleted the email property from our object by using the delete operator.
  3. Finally, we logged the object to the browser console so that we can see the result.

If you run the example above, you should see the following output in your browser console:

Remove a property from a JavaScript object

As you can see, the “email” property has been completely removed from our object.

Dynamically deleting a property from a JavaScript object.

If you need to dynamically delete a property from a JavaScript object, then you can use the “square bracket” syntax like so:

//An example object that represents a user
var user = {
    name: 'Wayne',
    dob: '1987-11-01',
    email: '[email protected]'
};

//Dynamically delete an object property
var propertyToDelete = 'dob';

//Delete it.
delete user[propertyToDelete];

//Log to console.
console.log(user);

Here, we were able to dynamically delete the property by using the bracket notation instead of the dot notation that we used in the first example.

Hopefully, you found this guide useful!