DEV Community

Cover image for The Lies of "===" operator

Posted on

The Lies of "===" operator

It is funny to know that there is two occasions where === returns a lying result.
The first is:

0 === -0 // True

To avoid this is useful to use, -0) // False

The second is:

NaN === NaN // false

To avoid this is useful to use isNaN()

Number.isNaN(NaN) // True

More about comparison operators MDN

Top comments (11)

bipinrajbhar profile image
Bipin Rajbhar

0 === -0 is true because 0 doesn't have sign

NaN === NaN is false because, according to IEEE 754 specifications any operation performed on NaN values should yield a false value or should raise an error.

val_baca profile image
Valentin Baca

-0 does in fact have a sign:

Moreover, there are two zero values, called signed zeros: the sign bit specifies whether a zero is +0 (positive zero) or −0 (negative zero).

craigmc08 profile image
Craig McIlwrath

That same Wikipedia article specifies that 0 and -0 should compare as equal.

lexlohr profile image
Alex Lohr

Actually, both are valid cases, since === compares the actual value and not the reference. Since primitives that contain the exact same value are stored within the same reference, will work; the value of 0 and -0 is the same, but since the sign differs, they are stored with different references.

And NaN is specifically meant not to work as a number, so it should not be calculatable or comparable - so if you put garbage into JS numbers, you'll get garbage out, even if you add non-garbage numbers. will work again here, because the reference to both instances of NaN is still the same.

So it's not a lie, even though the result might be unexpected if you are not familiar with the spec of the language.

patarapolw profile image
Pacharapol Withayasakpunt • Edited

When will you need to compare 0 and -0, as both are just floating points?

For NaN, well, there is nothing I can do about it...

giandodev profile image

Always when you decide to use 0 and -0 in your program. I hope you never have to use -0 but it's up to you 🙂.

patarapolw profile image
Pacharapol Withayasakpunt

0.1 + 0.2 !== 0.3 is headache enough, but now just let 1 - 1 === -1 + 1, please.

slax0rr profile image
Tomaz Lovrec

Why would one HAVE to use -0? -0 is flawed, since, 0 is neutral, it doesn't have a sign.

Take 1 for example, 1 == +1 since 1 has the + sign, where as with 0, is not +0 because 0 has no sign. So 0 === -0 being true is actually one of the few places where JS is right.

Thread Thread
giandodev profile image

Hi Tomaz, I’m not talking about why ... I just show a fact 🙂 I never use -0 in my life but I am not God I am just a person.
Use or not it’s up to you 🙂

louy2 profile image
Yufan Lou • Edited

Another day, another developer discovers IEEE 754 double-precision floating-point standard.

As exhibit A I present to you

0.1 + 0.2 === 0.3  // false


andrewkeyboardwarrior profile image
Andrew Gibson

Did you also get the question about what is the difference between "==" and "===" at your first JavaScript interview?