You Don't Know JS Yet: Get Started by Kyle Simpson

You Don't Know JS Yet: Get Started by Kyle Simpson

Author:Kyle Simpson
Language: eng
Format: mobi, epub
Publisher: GetiPub & Leanpub
Published: 2020-01-27T22:00:00+00:00


In the case of NaN, the === operator lies and says that an occurrence of NaN is not equal to another NaN. In the case of -0 (yes, this is a real, distinct value you can use intentionally in your programs!), the === operator lies and says it’s equal to the regular 0 value.

Since the lying about such comparisons can be bothersome, it’s best to avoid using === for them. For NaN comparisons, use the Number.isNaN(..) utility, which does not lie. For -0 comparison, use the Object.is(..) utility, which also does not lie. Object.is(..) can also be used for non-lying NaN checks, if you prefer. Humorously, you could think of Object.is(..) as the “quadruple-equals” ====, the really-really-strict comparison!

There are deeper historical and technical reasons for these lies, but that doesn’t change the fact that === is not actually strictly exactly equal comparison, in the strictest sense.

The story gets even more complicated when we consider comparisons of object values (non-primitives). Consider:

[ 1, 2, 3 ] === [ 1, 2, 3 ]; // false

{ a: 42 } === { a: 42 } // false

(x => x * 2) === (x => x * 2) // false



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.