Delete Operator in JavaScript

Learn How to Delete Property from Object using the Delete Operator. The Delete Operator can be used to delete own property of an object or an element from an array.

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

  1. object is the name of an object, or an expression evaluating to an object. Note that expression must evaluate to a object
  2. property is the property to delete.

Note that

  1. You can delete a Property or Method of an object using the Delete Operator. It will completely remove the Property from the collection.
  2. Delete Operator can also be used to delete a array element. But this will not re arrange the array, but converts it into an sparse array
  3. Delete will not delete the regular variables or functions
  4. You can only delete the property owned by the object. You cannot delete the inherited properties (i.e. Prototype Properties)
  5. It also does not remove the Non-configurable properties.
  6. 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, functions or function parameters using the delete Operator.

The variable declared with var is attached to the global object, but it is a not configurable property. Hence it cannot be deleted.

The let or const variables are not attached to any object. Hence they cannot be deleted.

Trying to delete a variable

We cannot delete obj here as it is declared with let

obj is declared without let, var or const. Hence the delete operator will delete it.

The code below deletes the property a from the obj. because a is own property of the obj

In the following code, we pass the obj.a to the function deleteProperty1. But it will not delete the property. This is because function does not know which object o belongs to.

To delete the property in a function, we need to pass both the object along with the property name as shown below

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.

Deleting Array Elements

Delete Operator can also be used to delete an element in the array. JavaScript does not re arrange the array, but converts it into an sparse array. “sparse” array where the length is greater than the number of items or at least one item is missing at any of the index.

The code below deletes the element at index 2 (nums[2]) from the nums array. The Actual elements in array now becomes 3 but the length of the array does not become 3 but stays at 4. It is just that element at index 2 is removed and hence there is no value at that position. i.e. there is a hole in the array.

You can see that the when we loop through the array using the forEach it returns only 3 elements.

But remember that the JavaScript returns nums[2] as undefined although the index 2 does not contain any value including undefined

The nums1 & nums2 arrays appears same below, but they are not. The nums1 array is a sparse array with no value at index 2, but the nums2 is not a sparse array because it has a value undefined at index 2.

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.

References

Delete

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top