How to Check for NaN in JavaScript Using Isnan Function
Learn the difference between isNaN and Number.isNaN, why NaN !== NaN, and the right way to check for NaN in different situations with practical examples.
You cannot check for NaN in JavaScript with comparison operators, because NaN is the only value that is not equal to itself. You need dedicated functions to detect it instead.
JavaScript provides two functions for this: the modern Number.isNaN and the older global isNaN. They behave differently, and knowing which one to use prevents subtle bugs in your code.
let result = 0 / 0;
console.log(result === NaN); // false (never works)
console.log(Number.isNaN(result)); // true (correct way)The first check always returns false no matter what result holds because NaN is the only value not equal to itself. The second check correctly identifies it.
Never compare anything to NaN directly. Always use one of the dedicated checking functions.
Number.isNaN: The Strict Check
Number.isNaN was introduced in ES6. It returns true only if the argument is the actual NaN value, without any type conversion:
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN(0 / 0)); // true
console.log(Number.isNaN("hello")); // false (it is a string, not NaN)
console.log(Number.isNaN(undefined)); // falseUse Number.isNaN when you want to know whether a specific value is literally NaN. This is the right choice in most situations, especially when you already know the value should be a number.
isNaN: The Coercing Check
The global isNaN function has existed since the earliest versions of JavaScript. Before checking, it converts the argument to a number. This means many non-number values also return true.
console.log(isNaN(NaN)); // true
console.log(isNaN("hello")); // true
console.log(isNaN(undefined)); // trueThese extra true results are misleading. The string "hello" is not NaN, but isNaN converts it to NaN first, so it reports true. Values that convert to valid numbers return false:
console.log(isNaN("42")); // false
console.log(isNaN(true)); // false
console.log(isNaN(null)); // falseThe conversion step makes isNaN report true for strings, undefined, and objects, even though none of these are actually NaN. This can be misleading if you only want to check for the NaN value itself.
However, isNaN is useful when you want to know whether user input or external data can be treated as a valid number. If isNaN returns false, you know the value can be safely used in math operations.
Choosing the Right Function
The choice depends on what question you are asking. The following table summarizes when to use each function:
| You want to know | Use | Example |
|---|---|---|
| Is this value exactly NaN? | Number.isNaN | Number.isNaN(result) |
| Can this be used as a number? | !isNaN | !isNaN(userInput) |
| Is this a valid safe number? | typeof + !Number.isNaN | typeof v === "number" && !Number.isNaN(v) |
For form validation where the user typed a string, isNaN is appropriate because you want to know whether the string can become a number. For checking the result of a math operation, Number.isNaN is correct. You expect a number and want to detect failure:
function calculateSquareRoot(input) {
let num = Number(input);
if (Number.isNaN(num)) {
return "Please enter a valid number";
}
return Math.sqrt(num);
}The function first converts the input to a number with Number(). If the conversion fails, Number.isNaN catches it. Then it validates the numeric value itself before performing the math operation.
Checking User Input Safely
When processing user input from a form field, the value is always a string. You need to convert it to a number and check for failure. Here is a reliable pattern using Number() for conversion and Number.isNaN for validation:
function processAge(input) {
let age = Number(input);
if (Number.isNaN(age)) {
console.log("Not a valid number");
return;
}
console.log("Age is:", age);
}
processAge("25");
processAge("abc");Calling processAge with a valid numeric string and then with an invalid string produces two different console results, as shown in the output below:
Age is: 25
Not a valid numberThe Number function converts the string to a number. If the string is not numeric, it returns NaN. Number.isNaN catches this failure. Note that an empty string converts to 0, which may or may not be what you want depending on your validation rules.
For more on what NaN is and what causes it, see the NaN guide. For understanding how values convert between types, read the type coercion guide.
Rune AI
Key Insights
- Number.isNaN is the modern, strict way to check for NaN.
- isNaN converts the argument to a number first, which can be misleading.
- NaN === NaN is always false; never use equality to check for NaN.
- Use Number.isNaN for type safety; use isNaN for input validation.
- Guard numeric operations early to prevent NaN propagation.
Frequently Asked Questions
Which should I use: isNaN or Number.isNaN?
Can I check for NaN with === NaN?
How do I check if a value is a valid number and not NaN?
Conclusion
Use Number.isNaN for a strict NaN check that does not coerce its argument. Use the global isNaN only when you want to know if something can be converted to a valid number. Never use === NaN because NaN is never equal to itself. Guard your inputs early to catch NaN before it spreads through your calculations.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.