This article is going to discuss the delete operator available in JavaScript. Delete is comparatively a lesser-known operator in JavaScript. This operator is more specifically used to delete JavaScript object properties.
The JavaScript pop(), shift(), or splice() methods are available to delete an element from an array. But because of the key-value pair in an object, deleting is more complicated. Note that, the delete operator only works on objects and not on variables or functions.
Syntax:
delete object // or delete object.property // or delete object['property']
Parameter: It does not take any parameter.
Return type: This operator returns true if it removes a property. While deleting an object property that doesn’t exist will return a true but it will not affect the object. Though while trying to delete a variable or a function will return a false.
Below are examples of the delete Operator.
Example:
javascript
let emp = { firstName: "Raj" , lastName: "Kumar" , salary: 40000 } console.log( delete emp.salary); console.log(emp); |
Output:
true {"firstName":"Raj","lastName":"Kumar"}
Example 2: Assuming an object called person has three key-value pairs (i.e. first name, lastName and phone). Now, using the delete operator to delete the phone property will return true.
javascript
let person = { firstName: "John" , lastName: "Doe" , phone: 12345 } console.log( delete person.phone); console.log(person); |
Output:
As the above picture shows, delete person.phone returns true and logging the person object shows that the phone property doesn’t exist anymore.
Let’s try applying the delete operator for deleting a variable and a function.
Example 3:
javascript
let num = 5; let sum = (a, b) => { return a + b; } console.log( delete num); //false console.log( delete sum); //false |
Output:
false false
Because the delete operator doesn’t work for variables or function, it returns false and the actual variables and functions remain untouched.
Another thing to keep in mind is that this operator doesn’t delete property value rather the property itself.
Example 4:
javascript
let person = { firstName: "John" , lastName: "Doe" , phone: 12345 } let phone = person.phone; console.log( delete person.phone); //true console.log(phone); //12345 |
As objects are reference types, so both the person.phone and phone variable will refer to the same memory address.
Output:
true 12345
The output shows that the delete operator has deleted the property but the value still exists on the memory.
Exception: Global variables can be removed using the delete operator. Because the global variables are properties of the window object and as delete works on objects, it’ll delete the variable.
Example:
javascript
toDelete = 5; // true console.log( delete toDelete); // toDelete is not defined console.log(toDelete); |
Without using the var, let or const keyword sets the variable as a global variable and it’ll work as an object property.
Output:
true Uncaught ReferenceError: toDelete is not defined
The delete toDelete returns true and trying to access the variable after deleting it throws a reference error as the variable is not defined anymore.
Deleting Array Values Using delete: JavaScript arrays are after-all objects. So, the delete operator can be used. But it’ll cause a problem because after deleting the element from the array, this operator will show the position as empty and it’ll not update the array length.
Example:
javascript
let arr = [1, 2, 3] console.log( delete arr[0]); //true console.log(arr); //[empty, 2, 3] |
Output:
So, using pop(), shift(), or splice() methods is clearly a better approach to deleting array elements.
Conclusion: There are other ways used by developers, such as setting the value of an object property to null or undefined. But the property will still exist on the object and some operators like for in loop will still show the presence of the null or undefined property.
Using the delete property in loops slows down the program significantly. So, this method should only be used when it is absolutely necessary to delete an object property.