JavaScript - Delete Operator
JavaScript - Delete Operator
JavaScript Delete Operator
The JavaScript delete operator deletes/ removes a property from an object. It removes the property as well as value of the property from the object. It works only with the objects not with the variables or functions.
In JavaScript, an array is an object, so you can use the βdeleteβ operator to delete an element from the particular index. However, there are methods like pop(), slice(), or shift() available to delete the element from the array.
Syntax
Follow the below syntax to delete the object property using the βdeleteβ operator.
delete obj.property;
OR
delete obj["property"];Return valueβ The βdeleteβ operator returns true if the operand (specified property) is deleted successfully, otherwise returns false if the property is not deleted.
If you try to delete a property that doesnβt exist, it will return true but will not affect the object.
Follow the below syntax to delete the array element using the βdeleteβ operator.
delete arr[index];Deleting Object Properties
The JavaScript delete operator can be used to delete a property of an object. To delete the property we write the delelte operator followed by the property of the object.
delete obj.propertyName;
or
delete obj["propertyNmae"];In the above syntas, object property named propertyName is being deleted from the object called obj.
Example: Deleting an Object Property
The βobjβ object in the example below contains the product, price, and color properties. We used the deleteβ operator to delete the price property from the object.
It will produce the following result β
The Mobile price is undefined and color is BlueNotice that when we access the deleted property, it returns undefined.
Example: Deleting a Nonexistent object Property
Try to delete a property that doesnβt exit. It will return true but doesnβt affect the original object.
The above program will produce the following result β
trueDeleting Array Elements
We can use thedeleteoperator to remove or delete an element from an array. To delete an element, we use delete keyword followed by array element. We can use square brackets ([]) to access the elements from the array.
Example
The below code contains the array of numbers. We used the βdeleteβ operator to delete the element from the 1st index of the array. In the output, you can observe that element from the array gets deleted, but the position of the other elements remains as it is. The array length also remains the same.
It will produce the following result β
10,,30,40,50,60
undefinedDeleting Predefined Object
The JavaScript βdeleteβ operator can delete the predifiend object such as Math, Date, etc. It is not advisable to delete predefined objects. Once deleted, you canβt access the properties of these objects.
Example: Deleting Built-in Math Object
In the example below, we try delete Math object so we get the above error.
It will produce the following output β
delete Math.PI :false
delete Math :true
ReferenceError: Math is not definedCanβt Delete Variables and Functions
The delete operator canβt delete the variables or functions.
It will produce the following result β
false
falseThe variable defined without var, let or const can be deleted. Such a variable is treated as property of window object.
It will produce the following result β
true
ReferenceError: x is not defined