Skip to content Skip to sidebar Skip to footer

Why Delete Operator Returns True Even If Property Wasn't Deleted From An Object

>>> var par = {a: 1, b: 2}; undefined >>> var ch = Object.create(par); undefined >>> delete ch.a true >>> ch Object { a=1, b=2}

Solution 1:

You misunderstood what delete returns:

Throws in strict mode if the property is an own non-configurable property (returns false in non-strict). Returns true in all other cases. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete)

delete ch.a tries to locate the property a in ch, fails (since ch doesn't have such own property), does nothing and happily returns true. If you wrote delete ch.foobar, the result would be the same. If however, you tried a non-configurable property (e.g. delete ch.__proto__), the result would be false.


Post a Comment for "Why Delete Operator Returns True Even If Property Wasn't Deleted From An Object"