What happens in JavaScript if I have a variable, say:
var exampleObject = {one:'foo',two:'bar'};
and then I delete a property that doesn't exist, a la:
delete exampleObject.seven;
Is there a standard course of action that takes place everywhere (nothing, error message, script crashes, etc.), or is this dependent on some kind of implementation (browser engine, etc.)?
What happens in JavaScript if I have a variable, say:
var exampleObject = {one:'foo',two:'bar'};
and then I delete a property that doesn't exist, a la:
delete exampleObject.seven;
Is there a standard course of action that takes place everywhere (nothing, error message, script crashes, etc.), or is this dependent on some kind of implementation (browser engine, etc.)?
Share Improve this question asked May 6, 2013 at 20:31 thisissamithisissami 16.3k16 gold badges48 silver badges77 bronze badges 4- 9 Is it that hard to test? – Joseph Commented May 6, 2013 at 20:31
- 26 It's not obvious to me whether or not something that takes place is dependent on the environment or a standard across all implementations of the language. – thisissami Commented May 6, 2013 at 20:33
- 7 the world ends in an explosion of god's tears – nathan hayfield Commented May 6, 2013 at 20:34
- 1 developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/… – Tom Hubbard Commented May 6, 2013 at 20:34
2 Answers
Reset to default 49Nothing happens.
Assuming, x = {}
, Type(x.y)
is not a Reference Specifcation Type (there cannot be a "reference" to a property that does not exist). According to 11.4.1 The delete Operator, this satisfies the rule:
- Let ref be the result of evaluating UnaryExpression.
- If Type(ref) is not Reference, return true.
- ...
This behavior (of "no action") has existed for a long time - any environment that behaves differently is non-compliant. From the 3rd Edition ECMAScript Specification:
When the [[Delete]] method of O is called with property name P, the following steps are taken:
- If O doesn’t have a property with name P, return true.
- ..
If exampleObject is an object, the return value from delete is true, even if the property does not exist.