Alternative MDN Offer "Object.is"

I read the MDN page using the "Object.is" method . It provides alternative code for browsers that do not provide this method:

if (!Object.is) { Object.is = function(v1, v2) { if (v1 === 0 && v2 === 0) { return 1 / v1 === 1 / v2; } if (v1 !== v1) { return v2 !== v2; } return v1 === v2; }; } 

The question is simple: when can there be a true second β€œif”?

Thank you for your attention.

+7
javascript object methods ecmascript-6
source share
1 answer

This is somehow written in the same article :

This is also not the same as according to the === operator. The === operator (and the == operator) also considers the numeric values ​​-0 and +0 equal, and treats Number.NaN as not equal to NaN .

The logic is that NaN !== NaN is the only case when the !== operator returns true on the same variable, so it should be about NaN . Then it performs the same check on v2 and returns true for false based on the result: if v2 comparison is true , then about NaN compared to NaN they return true , if not, then return false , because NaN never matches what is not NaN.

+4
source share

All Articles