In JavaScript, the delete operator is employed to delete a property of an object. After deleting the actual property, that property won’t be accessible and returns undefined.
The invocation of the delete operator returns true when it removes a property and false otherwise. it’s only effective on an object’s properties, it has no effect on variable or function names.
The delete operator shouldn’t be used on predefined JavaScript object properties like window, Math, and Date objects. It can crash your application.
Let’s scrutinize some facts about the delete operator.
Delete object properties
The only way to fully remove the properties of an object in JavaScript is by using delete operator.
If the property which you’re trying to delete doesn’t exist, delete won’t have any effect and can return true.
Can we delete variables in Javascript?
The delete operator removes a property from an object. It cannot delete a variable. Any property declared with var cannot be deleted from the global scope or from a function’s scope.
If you declare a variable without var, it can be deleted. Let’s look into the example below.
The variable declared without the var keyword internally stores it as a property of the window object. So we can delete the properties of the window object.
Can we delete values from an array?
Since JavaScript arrays are objects, elements can be deleted by using delete.
delete will delete the object property, but will not reindex the array or update its length. This makes it appear as if it is undefined.
Using delete may leave undefined holes in the array. Use pop(), shift(), or splice() instead.
Deleting built-in objects like Math, Date, and window objects is unsafe, and it can crash your entire application.
Deleting non-configurable properties
Object properties, besides a value, has three special attributes:
writable – if true, the value can be changed, otherwise it’s read-only.
enumerable – if true, it is listed in loops, otherwise not listed.
configurable – if true, the property can be deleted or the attributes can be modified, otherwise it cannot be changed.
The values assigned by using Object.defineProperty and set to configurable: false in an object cannot be deleted.
In strict mode, it will throw an error if you try to delete a non-configurable property.
delete is the only true way to remove an object’s properties without any leftovers, but it works ~100 times slower if you are using delete in loops.
The alternative solution is setting the value to undefined like object[key] = undefined. It doesn’t fully delete the property, it just sets value to undefined. the choice isn’t exactly a prominent solution, but if you utilize it with care then you’ll be able to improve the performance.

Source Code: https://github.com/jayanthbabu123/5-things-about-delete-operator-in-javascript

Subscribe: https://www.youtube.com/channel/UCNVKOc0Ya-MVHElzxT7htxw

#js #javascript

5 Things You Need to Know About Delete Operator in JavaScript | Interview guide
2.50 GEEK