This content originally appeared on Bits and Pieces - Medium and was authored by John Troutman
Is it a Number or Not?
NaN is a property of the global object representing the failure of a mathematical statement. It literally means Not-A-Number. It can also be a pain in the butt to pin down.
Here’s why.
So NaN isn’t a number right? Seems obvious, I mean it’s in the name after all. A quick check of its data type will confirm this:

Rather ironic for the thing called “Not-A-Number” to actually be a number data type. So why is this? In JavaScript, as well as being part of the global object, it is also a property of Number.

For this reason NaN is actually considered a number. No worries if you’re a bit confused trying to wrap your head around this. NaN is a type of number so think of it as a number with an undefined value, one that is returned from an illogical math statement to indicate a problem with the calculation.
Okay, so NaN is actually a number. It’s value is similar to undefined so what happens if we check for NaN and add edge cases if it’s returned. Let’s give it a try.
That’s right, you’re not seeing things. Compared to itself NaN returns false. Whether using strict or regular equality the result is the same.

NaN is the only value in JavaScript that is not equal to itself. The reason for this is rather complex and has to do with the standard for floating-point arithmetic (IEEE 754). To put it very simply, when evaluating numbers in math the values are always greater than, less than, or equal. NaN is a type of number with a value that cannot be determined. Therefore it is not mathematically possible to say that the value of NaN is the same, greater, or less than another NaN value. I encourage you to check out this discussion for a much more in-depth explanation on the reasoning behind this.
Since NaN cannot be compared to itself how can a value be checked to see if it is NaN? There are two methods, the global isNaN() and Number.isNaN().
Both methods will return a boolean determining if the value passed is NaN. On the surface they look the same but they function differently.
Why does "100x" return true for isNaN() and false for Number.isNaN()? When the data type of the value given to the global isNaN() method is not a number it will first try to convert it to a number and then check if it is NaN.
The string "100x" cannot be converted to a valid number and subsequently is declared NaN and isNaN() returns true. The Number.isNaN() method does not convert the input value. Instead it returns true only if the data type is both a number and NaN. Because "100x" is a string and not a number the return value is false.
Due to the inherent conversion of inputs to numbers with isNaN() it is recommended to use Number.isNan() in most cases.
NaN can be tricky to understand. It stands for Not-A-Number but actually is a type of number. It is the only value in JavaScript that cannot be compared to itself. Its presence can only be checked with two methods that function differently and can return undesired results if misunderstood. The good news is knowing these concepts lays the groundwork for understanding what NaN is, how it works, and how to handle it.
Build composable web applications
Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favorite frameworks like React or Node. Build scalable and modular applications with a powerful and enjoyable dev experience.
Bring your team to Bit Cloud to host and collaborate on components together, and speed up, scale, and standardize development as a team. Try composable frontends with a Design System or Micro Frontends, or explore the composable backend with serverside components.

Learn More
- How We Build Micro Frontends
- How we Build a Component Design System
- The Composable Enterprise: A Guide
- How to build a composable blog
- Extendable UI Components
The Quirks and Peculiarities of NaN was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by John Troutman

John Troutman | Sciencx (2022-04-13T10:20:25+00:00) The Quirks and Peculiarities of NaN. Retrieved from https://www.scien.cx/2022/04/13/the-quirks-and-peculiarities-of-nan/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.