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
Maybe because of the type you think๐ค
NaN == Number.NaN // false
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
- Math operation where the result is not a real number
Math.sqrt(-1); โ NaN
- The operand of an argument is NaN
NaN + 20 โ NaN
60 * NaN โ NaN
- Indeterminate form
0 * Infinity โ NaN
- Any operation that involves a string and is not an addition operation
"Integer" * 5 โ NaN
For geekier discussions, reach out to me on twitter at @radnerus93, ๐ฅ DM always open.
Top comments (19)
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.Just to clear things up,
NaN
is just a specially formatted number defined by the IEEE 754 standard, hence having thetypeof
number
.NaN
simply represents a "floating-point arithmetic exception", but it is still laid out in memory as a floating-point number.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.
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. ๐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.
NaN isn't a numerical representation, because it isn't represented with numerals. :)
Let's call it, a tried and failed numerical representation ๐ just for the fun of it๐ฌ
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.
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!
I agree.
NaN
is really just an unfortunate (but equally appropriate) acronym for something that works like a number.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!
Good one, Diegoโจ
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.
I would be more careful when dealing with numbers in state of numbers now๐
It's probably worth noting that NaN === NaN is false, which is why there's isNaN to detect it.
True!
Oh wow, I just realized after reading this, that I have never really looked at what really is NaN.๐
Glad it helped you, Tanvesh! Now you know and that's great ๐
Great, thanks