DEV Community

Cover image for Why NaN !== NaN Makes Perfect Sense (I Promise)

Why NaN !== NaN Makes Perfect Sense (I Promise)

Sylwia Laskowska on January 25, 2026

No, JavaScript Isn’t Broken (Again) I’ll admit it — sometimes I like publishing a post on the weekend, when both the internet and real l...
Collapse
 
pascal_cescato_692b7a8a20 profile image
Pascal CESCATO

Awesome! Your WTF JS format is really refreshing. And yes, I wondered (a long time ago) why NaN === NaN returns false. You explain it, and I think to myself: "Yes, that's it!" as if there were no other possible explanation, as if it were obvious (for me, it wasn't).

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Thanks! 😄 And yes - it’s actually not a big secret at all, once you see it explained. It just feels mysterious until that “click” moment happens.

If you enjoy this kind of JS WTFs, I can highly recommend You Don’t Know JS Yet — it’s available for free on GitHub and makes for perfect long winter-evening reading!

Collapse
 
pascal_cescato_692b7a8a20 profile image
Pascal CESCATO

I just checked out You Don’t Know JS Yet — it does look interesting, but it doesn’t have that storytelling touch that makes your content stand out. And honestly, JavaScript isn’t enough of a passion for me to dive into something that heavy. I’d much rather follow your series; it’s much more refreshing!

Thread Thread
 
sylwia-lask profile image
Sylwia Laskowska

Haha, thanks Pascal 😄 that’s a huge compliment!

You’re totally right - You Don’t Know JS Yet is more of a deep-dive reference than a storytelling ride, and it’s definitely not for everyone.
Maybe I should team up with Kyle and do a light editorial pass: same knowledge, more emojis, fewer mental breakdowns 😂

Thread Thread
 
pascal_cescato_692b7a8a20 profile image
Pascal CESCATO

That's why I'm always drawn to your posts… and those of @aaron_rose_0787cc8b4775a0. Yours are truly refreshing, and his read like a captivating novel. With the two of you, I'm not really reading "technical articles"—I'm reading engaging articles.

Most of the time, I only delve into an article when it truly captivates me… the rest I skim. Yours always do.

Thread Thread
 
aaron_rose_0787cc8b4775a0 profile image
Aaron Rose

What a lovely thing to say, Pascal. I’m just glad to be here contributing to Sylwia’s excellent post. She sets a high bar for all of us! I always look forward to seeing your name in posts and comments. Cheers! 🙏✨❤

Thread Thread
 
sylwia-lask profile image
Sylwia Laskowska

Awwww @pascal_cescato_692b7a8a20 @aaron_rose_0787cc8b4775a0 you are too nice!!! 🥹🥰💖

Thread Thread
 
2colours profile image
2colours

It takes nothing from this post but I think You Don't Know JS is actually fairly entertaining and a considerably "light read" for something that technical

Thread Thread
 
pascal_cescato_692b7a8a20 profile image
Pascal CESCATO

You're right, and that's Sylwia's DNA… her writer's DNA, at least 🙃

Collapse
 
drake_kuwait_ddea6374d481 profile image
Drake Kuwait

Totally agree with you 👍

Collapse
 
codingpanel profile image
Coding Panel

Loved this explanation, Sylwia! 😄 NaN !== NaN always felt like one of those “JavaScript is broken” moments, but your breakdown makes it click—seeing NaN as “unknown” really clears things up. I also didn’t realize Object.is() could treat NaN as equal—super useful tip for numeric-heavy code! 👏

Collapse
 
ingosteinke profile image
Ingo Steinke, web developer

Fun and educational, and another reason to use either TypeScript or strict eslint recommendations.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Exactly! 😄
I honestly can’t even remember the last time I wrote something in pure JS… okay, I can - but I choose to repress that memory 😅

Collapse
 
ingosteinke profile image
Ingo Steinke, web developer

I remember a project using very strict and opinionated eslint rules, probably based on airbnb recommendations, but still EcmaScript, not TypeScript. That included a long list of idioms to avoid, like using the plus sign to concatenate strings (for the risk of mixing it with numbers) and even using i++ was discouraged for allegedly facilitating off-by-one errors.

Thread Thread
 
sylwia-lask profile image
Sylwia Laskowska

Oh yes 😄 I remember those Airbnb recommendations very well… and how many juniors cried over them 😅

Collapse
 
2colours profile image
2colours

What does Typescript bring to floating-point arithmetics?

Collapse
 
doogal profile image
Doogal Simpson

Great point bringing up IEEE-754. It’s easy to dunk on JavaScript for these quirks, but when you see the same behavior in Rust or C++, it shifts the conversation from 'weird language behavior' to 'foundational computer science.' It’s a reminder that as much as we abstract things away with high-level code, we’re still ultimately operating within the constraints of how silicon handles math.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Exactly! 😄 That’s precisely the point.
Once you see the same behavior in Rust or C++, it stops being “JavaScript being weird” and starts being “this is how floating-point math works, period.”

And honestly… I don’t know why JavaScript ended up as the main scapegoat here either 😂 It just happens to expose these rules more openly than some other languages.

Collapse
 
francistrdev profile image
👾 FrancisTRDev 👾

This is funny, but it makes sense in a technical standpoint.

The way I think "NaN" in my view is "There are many different categories that falls under NaN, in where NaN is not a universal things". Like your example where an emoji is NaN and a string "123" is a NaN.

Sure, both are NaN, but it is about what causes the NaN for NaN !== NaN to be true since they are different. Great job!

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Exactly - that’s a really good way to think about it 😄
Different “reasons” for NaN, same result, but not something you can meaningfully compare. Thanks a lot! 🙌

Collapse
 
htho profile image
Hauke T.

I like your Rust example, where the same variable is unequal to the same variable. LMAO

let x = f64::NAN;
x == x // false
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Exactly! 😄 It’s not a JS-only thing at all. NaN is just… consistently weird in many languages 😂

Collapse
 
bhavin-allinonetools profile image
Bhavin Sheth

This was a really fun read — the “unknown result” framing makes NaN finally click instead of just feeling like a weird JS prank.

I especially liked how you tied it back to IEEE-754 and showed it’s not a JavaScript-only thing. That context alone clears up so many “JS is broken” takes.

The JS WTF weekend format works great too — light, nerdy, and still genuinely educational. Looking forward to the next one.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Thank you so much! 😄 I’m really glad the “unknown result” framing helped it click — that’s exactly the moment I was hoping for.

And yes, the IEEE-754 context is such a quiet plot twist here — once you see it’s not just JavaScript, a lot of those “JS is broken” takes suddenly fall apart 😅

Happy to hear the JS WTF weekend format works for you — more nerdy WTFs coming soon! 🚀

Collapse
 
itsugo profile image
Aryan Choudhary

This was a really good read Sylwia. The “NaN as unknown, not a value” framing is one of those explanations that makes everything suddenly feel obvious in hindsight. It turns something that feels like a JS prank into a really clean mental model.

I also really liked learning about IEEE-754 and other languages, it quietly reframes this from “JavaScript weirdness” into “floating-point reality,” which is way more useful for how people actually think about bugs.

For future JS WTFs, are there any other “looks broken but is actually logical” behaviors you’re especially excited to cover?

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Thank you so much! 😄 I’m really glad that mental model landed - that “oh, of course” moment is the best kind of JS WTF outcome.

As for what’s next: I’m definitely thinking about some “weird” array and object behaviors 👀
And honestly… JavaScript itself is an endless source of inspiration for this series 😂

Collapse
 
mikulas_bartacek_cd3cacc2 profile image
Mikulas Bartacek

Nice! I knew that JS do this, but i didn't know why. I thought that it has to do something with value representation (object reference comparing etc...). Now i know that it's directly from IEEE 754. thanks

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Exactly — that’s a very common intuition 😊
Glad the IEEE-754 connection cleared it up. Thanks for reading! 🙌

Collapse
 
shemith_mohanan_6361bb8a2 profile image
shemith mohanan

This was a really enjoyable read — clear, light, and surprisingly satisfying 😄
The “NaN means unknown, not a value” framing makes everything click. I especially liked the point about languages choosing honesty over convenience. Great example of a JS “WTF” that actually makes sense once you zoom out.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Thank you! 😄 I’m really happy that framing resonated with you — “unknown, not a value” is one of those ideas that suddenly makes everything fall into place.

And yes, that trade-off between honesty and convenience is such a great example of how many so-called JS “WTFs” actually make a lot of sense once you zoom out a bit. Glad you enjoyed it! 🚀

Collapse
 
eduardobc88 profile image
Eduardo Beltran Carbajal

"unknown result" === "unknown result" should be "false" because "unknown result" itself is "unknown". So "unknown" === "unknown" is "false"

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Haha, exactly 😄
That’s the whole idea - if the result is truly “unknown,” even comparing it to itself can’t be trusted.

Collapse
 
kooiinc profile image
KooiInc

To summarize: all JS-WTF's make perfect sense 😉

To be complete: 15 years ago (time flies when you're having fun) I asked on stackoverflow why typeof NaN returns Number. Got some excellent answers over there.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Aaach, what a beautiful piece of archaeology! 😄
Time really does fly - thanks for sharing that!

Collapse
 
beltonalberto profile image
BeltonAlberto

Makes perfect sense... I always had the this thought, like "this must have some logical explanation...".
Thanks for the article

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Exactly 😄 - there is a logical explanation hiding underneath.
Thanks a lot for reading! 🙌

Collapse
 
benjamin_nguyen_8ca6ff360 profile image
Benjamin Nguyen

Nice! I love that you make your articles so personal with the emoji and ease to read :). C++ is the hard language to learn but it gets easier afterward. I love C++ and Python.

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Thank you! 😊 I’m really happy you like the personal tone and emojis - that means a lot to me.

And yes, C++ is definitely a tough one 😅 but once it “clicks,” a lot of things in other languages start to make much more sense. Great combo with Python too!

Collapse
 
benjamin_nguyen_8ca6ff360 profile image
Benjamin Nguyen

Lol! it is so true :). Thank you!

Collapse
 
amandamayfield profile image
Amanda Mayfield

I just love all the little niche wtf cases you make into ah, yes, that is indeed the fuck. Great article as always!

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

Thanks, Amanda! 😄
And yeah… JavaScript is way too generous when it comes to WTF material, so there will definitely be more of this coming 😂

Collapse
 
evanlausier profile image
Evan Lausier

So it's 'like' the strict assignment? See what I did there 😂

Collapse
 
sylwia-lask profile image
Sylwia Laskowska

😂 I see it - strict everything, except equality apparently!

Collapse
 
hugo_devreugd_42eda39309 profile image
Hugo de Vreugd

You could also add SQL to the list of languages: NULL <> NULL.

Collapse
 
martiserra99 profile image
Martí Serra Molina

Interesting insights!

Collapse
 
nra profile image
N. R. Ashwin

Waah! interesting way to explain the JS deep point.

Collapse
 
gokul_panwar_84a9ccb8ed48 profile image
Gokul Panwar

Informative 👏