Comparing a variable with itself

I came across this polyfill Array.prototype.includes. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes . Is there a reason for comparing variables with themselves on line 21.22?

if (searchElement === currentElement || (searchElement !== searchElement && currentElement !== currentElement)) { return true; } 
+6
source share
1 answer

Yes, this second operand || checks if searchElement and currentElement NaN - a value in JavaScript that is not === for itself. It is assumed that includes uses the SameValueZero equivalence SameValueZero , which is different from the Strict equality comparison algorithm ( === used) or the SameValue algorithm (used in Object.is ).

+10
source

Source: https://habr.com/ru/post/978714/


All Articles