If you work with numbers in JavaScript, you might see a strange value called NaN. At first, it looks confusing. However, once you understand it, NaN becomes very easy to handle.
So, what does NaN mean?
In simple terms, NaN means “Not a Number.” JavaScript returns NaN when it expects a number but cannot calculate a valid numeric result.
Now, let’s understand this step by step with simple examples.
What Does NaN Mean in JavaScript?
NaN stands for Not a Number. JavaScript uses this value when a mathematical operation fails to produce a proper number.
For example:
let result = 0 / 0;
console.log(result);
This code prints:
NaN
Here’s why this happens. Dividing zero by zero does not produce a real numeric value. Therefore, JavaScript returns NaN.
In other words, NaN signals that something went wrong in a number calculation.
When Does JavaScript Return NaN?
JavaScript returns NaN in several situations. Let’s look at the most common ones.
1. Invalid Mathematical Operations
First, you get NaN when you perform an impossible math operation.
For example:
let value = Math.sqrt(-1);
console.log(value);
Since you cannot calculate the square root of a negative number in normal math, JavaScript returns NaN.
2. Converting Non-Numeric Strings
Next, NaN appears when you try to convert a non-numeric string into a number.
For example:
let num = Number("hello");
console.log(num);
Because the word “hello” is not a number, JavaScript cannot convert it. As a result, it returns NaN.
However, if you try:
Number("25")
JavaScript successfully converts it into the number 25. Therefore, you only get NaN when conversion fails.
3. Undefined or Invalid Calculations
Sometimes, calculations with undefined values also return NaN.
For example:
let x;
console.log(x + 5);
Since x is undefined, JavaScript cannot perform proper addition. Because of this, the result becomes NaN.
Important: NaN Is Still a Number
Now here’s something surprising.
Even though NaN means “Not a Number,” JavaScript still treats it as a number type.
You can check it:
console.log(typeof NaN);
Output:
number
This might feel strange. However, JavaScript internally classifies NaN as a special numeric value.
So remember this: NaN is a numeric data type, but it represents an invalid number result.
Why NaN Is Not Equal to Itself
Here’s another interesting fact.
If you compare NaN with NaN, JavaScript returns false.
For example:
console.log(NaN === NaN);
Output:
false
This happens because NaN represents an unknown numeric value. Since it is unknown, JavaScript does not consider two NaN values equal.
Therefore, you should not use normal comparison to check NaN.
How to Properly Check for NaN
Instead of using == or ===, you should use Number.isNaN().
For example:
let value = Number("abc");
console.log(Number.isNaN(value));
Output:
true
This method safely checks whether a value is truly NaN.
In contrast, the global isNaN() function can sometimes give confusing results because it tries to convert values before checking.
So, in modern JavaScript, always prefer Number.isNaN().
Real-Life Example of NaN
Imagine you build a simple calculator.
A user enters two numbers, and your program adds them. However, if the user types “ten” instead of 10, your calculation fails.
For example:
let a = "ten";
let b = 5;
let result = Number(a) + b;
console.log(result);
Since “ten” cannot convert into a number, the result becomes NaN.
Because of this, you should always validate user input before performing calculations.
Common Mistakes Developers Make
Now let’s quickly look at common mistakes.
First, many beginners compare NaN using ==. However, this always fails.
Second, some developers forget to validate user input. As a result, unexpected NaN values appear in calculations.
Finally, some people misunderstand NaN as an error. In reality, it is not an error. Instead, it is a special numeric value that signals an invalid calculation.
Final Thoughts
So, what does NaN mean?
In JavaScript, NaN means Not a Number, and it appears when a numeric operation fails. Although it represents an invalid number result, JavaScript still treats it as a number type.
Most importantly, you should use Number.isNaN() to check it properly and always validate input before performing calculations.
Once you understand how NaN works, you can prevent many common bugs in your JavaScript code.
Read Also – this Keyword in JavaScript

