According to this MDN page, the delete keyword
Returns false only if the property exists and cannot be deleted. It
returns true in all other cases.
However, I see cases where delete returns true, despite the property not being deleted:
delete Window
delete alert
delete dir
delete console
delete 2
delete null
delete {}.x
...
In fact, almost all properties of window return true with delete, as can be seen by the running the following script in about:blank:
for(a in window) { if(delete window[a]) { console.log(a); } }
However, most properties of window do not actually get deleted. What is the true meaning of the returned value of delete? Why does it return true for properties it doesn’t delete?
(Note: I would be interested in references to Chromium code explaining the behaviour of delete.)
The window is a host object, one whose semantics are defined by the host environment, e.g. the browser.
deletewhen applied to properties of host objects is more complicated than when applied to native objects.Section 11.4.1 – The
deleteoperator saysso when a host object doesn’t support deletion or modification of a property, then it returns an unresolvable reference or a reference which pretends to be deleted. Either approach causes
trueto be returned in non-strict mode.