DEV Community

Cover image for NaN - It's Not A Number๐Ÿ™„
Suren
Suren

Posted on

NaN - It's Not A Number๐Ÿ™„

According to MDN,

NaN is a property of the global object. In other words, it is a variable in global scope.

The initial value of NaN is Not-A-Number โ€” the same as the value of Number.NaN. In modern browsers, NaN is a non-configurable, non-writable property. Even when this is not the case, avoid overriding it. It is rather rare to use NaN in a program.

Hear the value of NaN is Number.NaN. But, wait!

NaN === Number.NaN // false
Enter fullscreen mode Exit fullscreen mode

Maybe because of the type you think๐Ÿค”

NaN == Number.NaN // false
Enter fullscreen mode Exit fullscreen mode

Somewhat reasonable explanation started here when I followed the links.

With a lot of confusion around NaN, let us see how we could arrive at a value of NaN.
There are 5 different operations which could result in NaN.

  • Numbers that can't be parsed
parseInt('Integer') โ‡’ NaN
Number('%^@#') โ‡’ NaN
Enter fullscreen mode Exit fullscreen mode
  • Math operation where the result is not a real number
Math.sqrt(-1); โ‡’ NaN
Enter fullscreen mode Exit fullscreen mode
  • The operand of an argument is NaN
NaN + 20 โ‡’ NaN
60 * NaN โ‡’ NaN
Enter fullscreen mode Exit fullscreen mode
  • Indeterminate form
0 * Infinity โ‡’ NaN
Enter fullscreen mode Exit fullscreen mode
  • Any operation that involves a string and is not an addition operation
"Integer" * 5 โ‡’ NaN
Enter fullscreen mode Exit fullscreen mode

For geekier discussions, reach out to me on twitter at @radnerus93, ๐Ÿ“ฅ DM always open.

Top comments (19)

Collapse
 
adam_cyclones profile image
Adam Crockett ๐ŸŒ€

I have not really used NaN directly in years, I wonder why this is. Oh this reminds me, I found a method I either forgot about or had completely missed. Number.isInterger. โœจ learn a new thing every day.

Collapse
 
somedood profile image
Basti Ortiz

Just to clear things up, NaN is just a specially formatted number defined by the IEEE 754 standard, hence having the typeof number. NaN simply represents a "floating-point arithmetic exception", but it is still laid out in memory as a floating-point number.

Collapse
 
pentacular profile image
pentacular

Let's be clear that NaN is not a number in IEEE. :)

It is a value that is used as a code to indicate that a floating point exception has occurred and the value does not represent a number.

Collapse
 
somedood profile image
Basti Ortiz

But in memory, it is still stored as a floating-point number, hence the typeof number. Although it is not exactly a correct description, it is kinda correct from the viewpoint of low-level memory. The bits of the "number" are just specially formatted to indicate an arithmetic exception.

I just meant to clarify that typeof NaN === 'number' isn't as unfounded as it first seems. ๐Ÿ˜‰

Thread Thread
 
radnerus profile image
Suren

NaN is the numerical representation of a value which was tried to be cases as a number, evaluated as an expression or directly assign a value of NaN. So it's time would be a "Number'. But wouldn't make sense if 2 NaN is equal.

Thread Thread
 
pentacular profile image
pentacular • Edited

NaN isn't a numerical representation, because it isn't represented with numerals. :)

Thread Thread
 
radnerus profile image
Suren

Let's call it, a tried and failed numerical representation ๐Ÿ˜ just for the fun of it๐Ÿ˜ฌ

Thread Thread
 
pentacular profile image
pentacular

But in memory, it is still stored as a floating-point number, hence the typeof number.

Well, I don't think this quite follows.

Javascript could have specified that typeof NaN is not 'number' -- it isn't required because of any IEEE representation -- it could have just as easily decided that NaN is its own kind of thing, and said that typeof NaN === 'NaN'.

This is a kind of arbitrary decision, but you can think about the advantages and disadvantages of these decisions.

Personally, I think that it makes sense for typeof NaN to be 'number' because it is produced by the same kind of operations that produce numbers -- so it's something that you expect to pass through the same interfaces.

That is, things that expect 'number' type things should also expect NaNs.

But this isn't determined by IEEE or anything else really -- it's just something that the
people who developed Javascript decided because it was felt to be useful to most people most of the time.

Thread Thread
 
radnerus profile image
Suren

Great thought process! But typeof give the primitive types and they might have thought it's not worth having a separate type for a value that is used very less!

Thread Thread
 
somedood profile image
Basti Ortiz

I agree. NaN is really just an unfortunate (but equally appropriate) acronym for something that works like a number.

Collapse
 
diegolepore profile image
Diego Palacios Lepore

Hi Suren, nice article! :-)

After reading your post I decided to share my thoughts on the NaN special value here in the comments, but then I noticed that comment started to get bigger so I decided to write a post ๐Ÿ˜…

Here's the link to my post. I hope it helps ๐Ÿ™‚๐Ÿ‘

ยกKeep up the good work!

Collapse
 
radnerus profile image
Suren

Good one, Diegoโœจ

Collapse
 
thehanna profile image
Brian Hanna

I discovered this week that NaN is truthy. A bunch of state values were showing undefined in a React app. However, it used server side rendering to initialize the state, and they started as NaN, causing a bunch of things to flash from one value to the next when the state updated. That was a fun two hours.

Collapse
 
radnerus profile image
Suren

I would be more careful when dealing with numbers in state of numbers now๐Ÿ˜

Collapse
 
pentacular profile image
pentacular • Edited

It's probably worth noting that NaN === NaN is false, which is why there's isNaN to detect it.

Collapse
 
radnerus profile image
Suren

True!

Collapse
 
tanvesh01 profile image
Tanvesh sarve

Oh wow, I just realized after reading this, that I have never really looked at what really is NaN.๐Ÿ˜…

Collapse
 
radnerus profile image
Suren

Glad it helped you, Tanvesh! Now you know and that's great ๐Ÿ˜‰

Collapse
 
bawa_geek profile image
Lakh Bawa

Great, thanks