Numbers in JavaScript is a complex topic. One can say that the number in JS is easier to learn, considering that JavaScript has only one type of the number, not dividing numbers into various types (like most other programming languages do).
On the other hand, what if, for example, you try dividing 25 by 'banana'? It doesn't make much sense, right? Nevertheless, a skilled JS developer has an answer. 25 divided by banana is NaN
.
What is NaN in JS?
NaN
stands for Not a Number. NaN
is a value that is not a valid or a legal number, basically. At the same time, we can see that the NaN
belongs to the number type, using the typeof
operator.
console.log(typeof NaN);
// Output: number
The most accurate way to define NaN
is to combine these properties. Hence, NaN
is an abbreviation for "Not a Number"; it is a value that belongs to the number type being not a legal number. NaN
can also be used in the meaning of a property in some aspects. The global NaN
property is the same as the Number.Nan
property. We will return to this aspect a bit later.
Here is an example of the essential use of NaN
for non-sense examples in comparison to the mathematical problem that has a valid number solution.
let a = 200 / 2 // Output: 100
let b = 25 / "banana" // Output: NaN
NaN
itself can't be used in math operations. If you try to summarize, let's say, NaN
and 10, the result will also be NaN
. It works with any math expression that uses NaN
.
let a = NaN;
let b = 10;
let summ = a + b; // Output: NaN
let c = NaN;
let d = "10";
let summ2 = c + d; // Output: NaN10
There are numerous scenarios where you can get NaN
as a result. For example, if you try to divide infinity
, another "exceptional" number in JS, by another infinity
, you will also get NaN
.
There are five main types of mistakes that will return NaN
as a result:
When number can't be parsed (
parseInt("text")
);Any math operation, where the result is not a real number (
Math.sqrt(-1)
);Using the
NaN
in the math operation as an operand (as it happened in previous examples);Using the indeterminate forms (
undefined
+undefined
, example withinfinity
divided byinfinity
, etc.);Involving the string as an element of math operation (It often happens after you tried to use the string that you incorrectly tried to convert into a number string).
How can you use NaN?
Obviously, NaN
is more often the result of the mistake than an expected result. If you saw NaN
, it mostly means that you need to track the moment that performed an operation that isn't valid for numbers and fix it. But can NaN
be helpful for you, and what can you do with it?
You know what to do if you see it as an indication of a failed operation on numbers. The best way to avoid the unexpected NaN
is to determine if the value or number is NaN
before starting an operation. We will take a closer look at two things that can presumably help us with this.
First, the global NaN
property is the same as the NumberNaN
. So, let's assume you use basic comparing and equating to check if the unit is NaN
. It may seem correct, but we will see exactly why it actually isn't in the next paragraph. The second thing is the isNaN()
or Number.isNaN()
method, the only correct way.
NaN === NaN; // Output: false
Number.NaN === NaN; // Output: false
isNaN(NaN); // Output: true
isNaN(Number.NaN); // Output: true
NumberNaN
property. The interesting feature of NaN
is that it never equals any value, including itself. So, it can't be used to see if a particular variable is NaN
or whether it isn't. In this sense, the usage of NumberNaN
is counterintuitive. That's why you better consider using the isNaN()
method in most situations when you need to determine if the value is a NaN
or not.
Number.isNaN and isNaN()
As it was previously said, there are two methods to determine if the given unit is NaN
. Both test the value and return true or false, depending on the result of the test. Still, there are some differentiations between Number.isNaN()
and isNaN()
:
The main difference is the way you should use them - .isNaN
returns true
if the value
is NaN
, while Number.isNaN
returns true if the number
is NaN
.
The difference is that you should apply Number.isNaN()
only for numbers, while the isNaN()
converts any value to a number before returning true
or false
.
isNaN(anytext); // Output: true
Number.isNaN(any text); // Output: false
Let's see how isNaN()
works with different values.
isNaN(undefined); // Output: true
isNaN({ }); // Output: true
isNaN(true); // Output: false
isNaN(null); // Output: false
isNaN(22); // Output: false
isNaN('22.5'); // Output: false (it was autoconverted from string to a number)
isNaN("22,5"); // Output: true
isNaN('222XYZ'); // Output: true (Number '222XYZ' is NaN)
isNaN(''); // Output: false (the empty string is autoconverted to 0, which is not NaN)