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:

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:

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!

Facebook Comments