The delete operator removes a property from an object. Its single operand should be a property access expression.
JavaScript: The Definitive Guide, 6th Edition
delete
is used to remove a properpty from an object.
But there are circumstances that we may expect the wrong result from it. Here’s what JavaScript: The Definitive Guide tells us.
A
delete
expression evaluates totrue
if the delete succeeded or if the delete had no effect (such as deleting a nonexistent property).delete
also evaluates totrue
when used (meaninglessly) with an expression that is not a property access expression:
delete
does not remove properties that have a configurable attribute offalse
. (Though it will remove configurable properties of nonextensible objects.) Certain properties of built-in objects are nonconfigurable, as are properties of the global object created by variable declaration and function declaration. In strict mode, attempting to delete a nonconfigurable property causes a TypeError. In non-strict mode (and in ECMAScript 3),delete
simply evaluates tofalse
in this case:JavaScript: The Definitive Guide, 6th Edition
As mentioned several times above, what is configurable?
The configurable attribute controls at the same time whether the property can be deleted from the object and whether its attributes (other than writable) can be changed.
Be careful when deleting on global objects. Here are some interesting results that may be confusing.
Why? How is x
different from y
in this case?
We know that y
, an undeclared variable can be used in almost the same way like declared variables like x
. Then, how are they different in this case?
Matt Coughlin gives a very clear answer to this question, which I’d suggest reading. To be brief, for declared global variables, the configurable attribute is false
. For undeclared global variables, it’s true
. And properties can be deleted only when it’s configurable
attribute is true
.