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.
Date objects as it can crash your application.
Let’s scrutinize some facts about the
Delete Object Properties
If the property that you’re trying to delete doesn’t exist,
delete won’t have any effect and can return
delete operator removes a property from an object. It can’t delete a variable. Any property declared with
var can’t 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
delete will delete the object property, but it will not reindex the array or update its length. This makes it appear as if it’s
delete may leave undefined holes in the array. Use
Deleting built-in objects like
window objects are unsafe, and they can crash your entire application.
Object properties, besides a
value, have three special attributes:
true, the value can be changed, otherwise, it’s read-only.
true, it’s listed in loops, otherwise, it’s not listed.
true, the property can be deleted or the attributes can be modified, otherwise, it cannot be changed.
Values assigned by using
Object.defineProperty and set to
configurable: false in an object can’t be deleted.
In strict mode, it will throw an error if you try to delete a non-configurable property.
Why Understanding the Delete Operator Is Valuable
delete is the only true way to remove an object’s properties without any leftovers, but it works significantly slower if you are using
delete in loops.
The alternative solution is setting the value to
object[key] = undefined. It doesn’t fully delete the property, it just sets the value to undefined. This option isn’t a prominent solution, but if you utilize it with care, then you’ll be able to improve the performance.