How to Remove a Property from a JavaScript Object?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

JavaScript objects contain properties that can be deleted if required. There are three ways using which we can easily delete any property of the object. The first way is using the special operator in JavaScript called the delete operator, the second way is using Object Destructuring and the third one is using the Reflect.deleteProperty() method.

Introduction

JavaScript Objects consist of properties that are just the combination of keys and values. In short, properties have pair of keys and values that can be of any kind of entity. Even functions can also act as a key or as a value in any Object. Anyway, here, we are going to discuss how can we remove key from Object JavaScript.

Removing a key automatically removes the value associated with that key. Hence, removing a key is nothing but removing that property itself. Now, there are three ways to remove key from object in JavaScript. Let us have a brief look over all of them.

1. Remove a Property from a JavaScript Object using the Delete Operator

There is a special operator in JavaScript called the delete operator which can be used to remove a key from Object JavaScript. As the name suggests, the delete operator simply deletes the specified property from the object. But first, we need to access the property to delete it. The Object property can be accessed either using the dot property accessor or the square brackets property accessor.

Deleting using the Dot Way

Syntax:

Example:

Let us create an employee object called emp that will be used for further operations.

Output:

Let us delete the age property using the dot way.

Output:

Explanation:

In the above-given output, we can see that the property age of the object emp has been deleted successfully using the delete operator(dot way).

Deleting using the Square Brackets Way

Syntax:

Let us delete the designation property of the same object emp using the square brackets way.

Output:

Explanation:

In the above-given output, we can see that the property designation of the object emp has been deleted successfully using the delete operator(square brackets way).

2. Remove a Property from a JavaScript Object using Object Destructuring

Object Destructuring can also be used for removing a property from an object, but there's a catch, Object Destructuring does not mutate(or change) the original object, instead creates a new object which does not consist of the deleted property. This way the original object remains intact(does not change).

Syntax:

Example:

Output:

Explanation:

In the above-given example, we used object destructuring to remove a property from an object. When we used object destructuring on the original object laptop to remove the property model from it, we got the new object myLaptop without that property. Again, we can see in the output that the original object did not change.

3. Remove a Property from a JavaScript Object using the Reflect.deleteProperty() method

The Reflect.deleteProperty() method is provided by one of the built-in JavaScript objects called 'Reflect'. This method is like the function form of the delete operator that we already discussed.

Example:

Output:

Explanation:

In the above-given example, we had an object called 'cars' with three properties. We used the Reflect.deleteProperty() method to delete the car2 property from that object. The output proves that the car2 property has been successfully deleted from the cars object.

At last, I would like to add that it does not matter whether you use the delete operator or Reflect.deleteProperty() method as both of them do the task in almost same time compexity. But using Object Destructuring consumes more time comparatively. Hence, the delete operator is the most preferred way of removing key from javascript object due to its short syntax and less time complexity.

Ready to dive into the world of web development? Join our JavaScript full course and unlock the secrets of building interactive websites.

Conclusion

  • JavaScript Objects consist of properties that are just the combination of keys and values.
  • Removing a key automatically removes the value associated with that key.
  • There are three ways to remove key from object in JavaScript.
  • We can remove a Property from a JavaScript Object using the delete Operator, Object Destructuring, and Reflect.deleteProperty() method.
  • The delete operator and the Reflect.deleteProperty() method deletes the specified property from the original object.
  • This means that the delete operator and the Reflect.deleteProperty() mutates the original object.
  • The Object Destructuring does not mutate the original object instead it creates a new object which does not contain the deleted property.