How to delete a property from a JavaScript object.

In this tutorial, we will show you how to delete a property from a JavaScript object.

To do this, we will be using the delete operator.

Take a look at the following example:

//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:

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

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

Remove a property from a JavaScript object

As you can see, the “email” property no longer exists.

Dynamically deleting a property from a JavaScript object.

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

//Example object
var user = {
    name: 'Wayne',
    dob: '1987-11-01',
    email: '[email protected]'
};

//The name of the property that we want to delete
var propertyToDelete = 'dob';

//Dynamically delete it.
delete user[propertyToDelete];

//Log the result to the 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.