Delete Property from Object: Delete Operator in JavaScript

This tutorial shows you How to Delete Property from Object JavaScript using the Delete Operator.

Delete Operator

You can delete a Property from an Object using the Delete Operator. The Syntax of the delete operator is as shown below

Where expression must evaluate to a Property. You can use both dot notation or square brackets notation in the expression.

Note that

  1. You can only delete a Property or Method of an object using the Delete Operator. It will completely remove the Property from the collection.
  2. Delete will not delete the regular variables or functions
  3. You can only delete the property owned by the object. You cannot delete the inherited properties (i.e. Prototype Properties)
  4. It also does not remove the Non-configurable properties.
  5. Delete returns false if the property is an own property and cannot be deleted. In all other cases it returns true.

Try to Avoid adding or deleting the Property of an object after it is created. The JavaScript engines optimize the performance of instances created by constructors. Adding or deleting prevents that optimization

Delete Property from Object

In the following example, we use the delete operator to delete the foo property the object obj.

The delete will remove the property from the collection. Hence you will not see the property when using the console.log on obj

Deletes Only Own Properties

Delete Only deletes the own properties. It does not delete the inherited properties.

The own property is a property that we declare directly on the object. The objects can also inherit properties from their prototype object. You can check if a property is an own property or not by using the hasOwnProperty method

The following example, create a obj1 from the constructor function obj. Here foo & boo are the own properties of the obj1. It does not define toString but inherits it from its Prototype. Hence we cannot delete it.

Another interesting point to note that the delete operator returns true, although it does not delete anything.

The toString function belongs to the Object.prototype. Hence we can delete it from there.

Deleting predefined JavaScript methods like toString is not a good idea. It will crash the parts of the app (or third-party libraries) which use it.

Deleting Non Configurable Properties

Delete will not delete Non Configurable own Properties.

We can create a Non Configurable property using the defineProperty method of the object. The following example creates two properties canDelete & cannotDelete. In cannotDelete, we set configurable: false.

Cannot delete functions & variable

You cannot delete regular variables or functions using the delete Operator.

Trying to delete a variable

The return value of delete

delete returns false if the property is an own property, but cannot be deleted. It returns true in all other cases. It will return true even if does not delete anything

Following are some examples.

In the following example, we try to delete a property that does not exist. Delete Operator returns true, although it does not delete anything

Delete & Strict Mode

In strict mode deleting a non-configurable property throw a type error.

Watch out for Undefined

Trying to access and property that does not exist returns undefined. Hence deleted Property returns undefined.

Also, note that you can set a property to undefined, the Property still exists but has the value undefined. Hence merely checking for undefined does not tell you whether the property does exist or not.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top