DEV Community

Discussion on: What is the type of NaN?

val_baca profile image
Valentin Baca • Edited on

c/o "You Don't Know JS"

NaN isn't equal to itself. NaN is the only value in the whole language where that's true; every other value is always equal to itself.


if (!Number.isNaN) {
    Number.isNaN = function(n) {
        return n !== n;

Weird, huh? But it works!

Despite the (condescending) name, the YDKJS series is actually really great.

vicoerv profile image

did the interpreter work like this.

if (token[0] == 'NaN') return false;

return token[0] == token[1];

just kidding

clarity89 profile image
Alex K. Author • Edited on

Haha, well it's more like each NaN has a different value under the hood, so technically they're not equal to each other.

Fun fact: since most of the bits in NaN encoding aren't used to store any meaningful information, they can be manipulated to store actual data - payload. This is called NaN-tagging or NaN-boxing and is mostly used for adding additional type encodings to values. Here's one interesting article about it.

Thread Thread
vicoerv profile image

wow thank you, never thought about that