JavaScript delete Operator

09 Sep, 2020
Below is the example of the delete Operator.
Example:<script>     let emp = {         firstName: "Raj",         lastName: "Kumar",         salary: 40000     }       console.log(delete emp.salary);    console.log(emp);</script> 
<script>     let emp = {         firstName: "Raj",         lastName: "Kumar",         salary: 40000     }       console.log(delete emp.salary);    console.log(emp);</script> 
Output:true
{"firstName":"Raj","lastName":"Kumar"}
true
{"firstName":"Raj","lastName":"Kumar"}
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, the 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']
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.
Example: Assuming an object called person has three key-value pairs (i.e. firstName, lastName and phone). Now, using the delete operator to delete the phone property will return true.
<script>    let person = {        firstName: "John",        lastName: "Doe",        phone: 12345    }      console.log(delete person.phone);    console.log(person);</script>
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.
<script>    let num = 5;    let sum = (a, b) => {        return a + b;    }      console.log(delete num); //false    console.log(delete sum); //false</script>
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:
<script>    let person = {        firstName: "John",        lastName: "Doe",        phone: 12345    }      let phone = person.phone;      console.log(delete person.phone); //true    console.log(phone); //12345</script>
As objects are reference type, 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:
<script>    toDelete = 5;      // true    console.log(delete toDelete);      // toDelete is not defined    console.log(toDelete);</script>
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:
<script>    let arr = [1, 2, 3]      console.log(delete arr[0]); //true    console.log(arr); //[empty, 2, 3]</script>
Output:
So, using pop(), shift() or splice() methods are clearly a better approach to delete 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.
javascript-operators
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.