I stumbled over this polyfill of Array.prototype.includes. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes. Is there a reason for the comparison of the variables with themselves on line 21,22?
if (searchElement === currentElement || (searchElement !== searchElement && currentElement !== currentElement)) { return true; }
Yes, this second operand of the || does check whether both searchElement and currentElement are NaN - the only value in JavaScript that is not === to itself. includes is supposed to use the SameValueZero equivalence algorithm, which is different from the the Strict Equality Comparison Algorithm (used by ===) or the SameValue algorithm (used in Object.is).
||
searchElement
currentElement
NaN
===
includes
SameValueZero
SameValue
Object.is
2.1m questions
2.1m answers
60 comments
57.0k users