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.

5 min read

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.

javascriptjavascript
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:

javascriptjavascript
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)); // false

Use 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.

javascriptjavascript
console.log(isNaN(NaN));       // true
console.log(isNaN("hello"));   // true
console.log(isNaN(undefined)); // true

These 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:

javascriptjavascript
console.log(isNaN("42"));      // false
console.log(isNaN(true));      // false
console.log(isNaN(null));      // false

The 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 knowUseExample
Is this value exactly NaN?Number.isNaNNumber.isNaN(result)
Can this be used as a number?!isNaN!isNaN(userInput)
Is this a valid safe number?typeof + !Number.isNaNtypeof 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:

javascriptjavascript
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:

javascriptjavascript
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:

texttext
Age is: 25
Not a valid number

The 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

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.
RunePowered by Rune AI

Frequently Asked Questions

Which should I use: isNaN or Number.isNaN?

Use Number.isNaN for most cases because it does not coerce the argument. It only returns true for actual NaN values. Use isNaN only when you intentionally want to check whether something can be converted to a valid number.

Can I check for NaN with === NaN?

No. NaN is never equal to anything, including itself. The expression value === NaN always returns false. You must use Number.isNaN(value) or isNaN(value) instead.

How do I check if a value is a valid number and not NaN?

Use typeof value === 'number' && !Number.isNaN(value). This checks both that the value is a number type and that it is not NaN. For user input strings, use !isNaN(Number(value)) to first convert then check.

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.