But, what if you’re trying to delete the property that doesn’t exist? In that case, the delete operator won’t have any effect and can return true.
Finally, the invocation of the delete operator returns true when it removes a property – when the deletion is successful, and false if the deletion isn’t successful, which may happen for various reasons.
A rule of thumb is that non-configurable properties cannot be removed, and this includes properties of built-in objects like Math, Array, Object and properties that are created as non-configurable with methods like Object.defineProperty().
You cannot remove variables with a delete operator
Can you delete values from an array?
What about built-in objects – can you delete them?
We’ve already mentioned that in this blog post – yes, you can delete built-in objects like Math, Date, and window. However, this practice is not safe, because deleting built-in objects can crash your application.
What about deleting non-configurable properties – can you delete them?
You should understand that object properties besides a value, have three special attributes:
writable – when true, you can change the value of a property, if not it’s read-only.
enumerable – when true, the property is listed in loops, if not it is not listed.
configurable – when true, you can delete the property or you can modify the attributes; if not true when called, it cannot be changed.
Deleting built-in objects and non-configurable properties is not safe – it can either crash your application or you can expect an error.
If you don’t want any leftovers in an object’s properties after deleting them, be aware that if you use delete in loops, it will work a lot slower. However, there is a solution. You can set the value to undefined like object[key] = undefined. Yes, that won’t delete the property with all leftovers, but it will set the value to undefined, and yes, that is not the best solution, but if you use it carefully, you should be able to improve the performance.