I was poking around the lodash repo and found the following:
if (isCommon && computed === computed) {
<stuff>
}
Here,
isCommonis a boolean initially set totrueand then reset tofalseconditionally on some checks;- while
computedis obtained by iterating over an array (for (let value of array)) and possibly applying a functioniterateeto 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?