Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the purpose of the following expression: (a !== a && b !== b) in angularjs code? [duplicate]

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;
}
like image 828
stoeffel Avatar asked Nov 26 '14 08:11

stoeffel


1 Answers

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).

like image 170
Bergi Avatar answered Oct 23 '22 10:10

Bergi