'In' Operator Avoids False Falsies
This line contains four assumptions, and therefore four places it can fail:
myObjectcontains a property called
datais an array
Presuming that if
myObject does not exist means you have bigger problems, it is easy to fall into the trap of checking for the existence of a property by gating a conditional with it:
1 2 3
myObject.data to contain derived information, you have signed up to troubleshoot some unexpected behaviour.
in operator checks if the key exists in the object’s hash table, and doesn’t care if it is a prototype or singleton property.
1 2 3 4 5 6 7 8 9 10 11
('data' in myObject) returns false if the property is undefined. If the property is defined, it returns true even if data contains a falsy value.