I was poking around the lodash repo and found the following:
if (isCommon && computed === computed) {
<stuff>
}
Here,
isCommon
is a boolean initially set totrue
and then reset tofalse
conditionally on some checks;- while
computed
is obtained by iterating over an array (for (let value of array)
) and possibly applying a functioniteratee
to the current valuevalue
.
My question:
Why is that if
clause not simply if (isCommon)
?
Elaboration (of one possible answer)
The Mozilla
operator-precedence table tells me that
- strict equality
===
has precedence 11, - higher than the precedence of 7 for the
&&
operator
This means, presumably, that the boolean check inside the if
is parsed as isCommon && (computed === computed)
.
Most of the time that second arm will be true, as x === x
even for funky "values" like null
or undefined
. The only other option I can think of is that applying the function iteratee
to value
to produce computed
might make a NaN. In that case the strict equality will evaluate to false; in node
:
> x=Math.abs("boo"); 1 && x === x
false
Is this it? Is the somewhat strange (to me) shape of the if
clause meant to guard against this eventuality?