Threesome !! ensures that the conclusion is true or false.
The expression obj && .. will result in the value obj when obj evaluates to false-y (for example, "" or 0).
Sometimes the input objects are not related to the result, and this "casting" (this is not casting at all, but rather coercion) clears the API and avoids leakage of details - we can guarantee that only true or false is returned.
Here is the TTL for a && b , note that the result is not necessarily true or false:
aba && b
Here is the TTL for !e , the result is always true or false:
e !e !!e ------- ------ ------ TRUTH-y false true FALSE-y true false
An alternative way to express the original expression, which I often use:
return obj ? obj.nodeType === 1 : false;
source share