What Is NaN in JavaScript? A Complete Not a Number Guide

NaN stands for Not a Number. Learn what causes NaN in JavaScript, its strange behaviors like NaN !== NaN, and how to handle it correctly in your code.

5 min read

NaN in JavaScript stands for Not a Number. It is a special numeric value that gets produced when a math operation fails to produce a meaningful number. Despite its name, NaN is actually part of the number type.

It signals that a calculation went wrong somewhere.

You typically encounter NaN when you try to perform arithmetic on something that cannot be converted to a number, or when a math operation itself has no valid numeric result.

javascriptjavascript
let result = "hello" * 3;
console.log(result);        // NaN
console.log(typeof result); // "number"

The string "hello" cannot be multiplied, so JavaScript produces this special number value. The typeof check confirms it belongs to the number type.

It is not a separate type. It is the number type's way of saying the result makes no sense.

What Causes This Value

This special number appears in a few specific situations. Knowing them helps you prevent it from showing up in unexpected places.

The most common cause is arithmetic on non-numeric strings:

javascriptjavascript
console.log("abc" * 2);   // NaN
console.log("abc" / 2);   // NaN
console.log("abc" - 2);   // NaN

Notice that addition with a string does not produce this value. Instead, JavaScript performs string concatenation. This is an important asymmetry in JavaScript: addition treats strings as the preferred type and concatenates, while other arithmetic operators try to convert everything to numbers first.

Parsing strings that do not start with a valid number also produces the same special numeric value. Both parseInt and parseFloat return it when they cannot find a number at the start:

javascriptjavascript
console.log(parseInt("xyz"));   // NaN
console.log(parseFloat("abc")); // NaN

Math operations with undefined produce this special value because undefined cannot be converted to a meaningful number. This often happens when a counter or accumulator was declared but never initialized:

javascriptjavascript
let total;
console.log(total + 10); // NaN (total is undefined)

Certain math functions return this special value for invalid inputs. Math.sqrt of a negative number produces it because there is no real square root:

javascriptjavascript
console.log(Math.sqrt(-1)); // NaN

NaN Is Not Equal to Itself

The most surprising behavior of NaN is that it is never equal to anything, not even itself. This is by design in the IEEE 754 floating-point standard that JavaScript follows.

javascriptjavascript
console.log(NaN === NaN); // false
console.log(NaN == NaN);  // false

This behavior exists because NaN represents an unknown or invalid number. Two unknown values cannot be assumed equal. The standard takes the conservative position that NaN is never equal to any value.

Because of this, you cannot use comparison operators to check for NaN. Checking result === NaN always returns false, even if result is NaN. You must use the dedicated functions Number.isNaN or isNaN instead.

How to Check for NaN

Use Number.isNaN for a strict check that only returns true for actual NaN values. It does not convert the argument to a number first:

javascriptjavascript
console.log(Number.isNaN(NaN));       // true
console.log(Number.isNaN("hello"));   // false (it is a string, not NaN)
console.log(Number.isNaN(undefined)); // false
console.log(Number.isNaN(42));        // false

The older global isNaN function converts the argument to a number before checking. This leads to surprising results where non-number values are reported as NaN:

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

For a detailed comparison of these two functions and when to use each, see the guide on how to check for NaN in JavaScript.

NaN Spreads Through Calculations

Once NaN enters a calculation, it spreads. Any math operation involving NaN produces NaN as the result. This makes NaN dangerous in chains of computations:

javascriptjavascript
let result = 10 + NaN;
console.log(result); // NaN
 
result = NaN * 5;
console.log(result); // NaN
 
result = Math.max(1, 2, NaN, 4);
console.log(result); // NaN

Because NaN propagates, a single bad input can poison an entire calculation. This is why you should check for NaN early, especially when processing user input or data from external sources. Guarding your math at the entry point prevents NaN from cascading through your application.

For more on how JavaScript handles different types in operations, see the type coercion guide.

Rune AI

Rune AI

Key Insights

  • NaN stands for Not a Number and is produced by invalid math operations.
  • typeof NaN returns 'number' because NaN is technically a numeric value.
  • NaN is never equal to anything, not even itself.
  • Use Number.isNaN() to check for NaN, not isNaN().
  • NaN in any calculation produces more NaN, so catch it early.
RunePowered by Rune AI

Frequently Asked Questions

Why does typeof NaN return 'number'?

NaN is technically part of the number type in JavaScript. It represents the result of an operation that was supposed to produce a number but could not. It follows the IEEE 754 floating-point standard where NaN is a numeric value signaling an invalid result.

Why is NaN === NaN false?

By design in the IEEE 754 standard, NaN is never equal to anything, including itself. This is because NaN represents an unknown or invalid number, and two unknown values cannot be assumed equal. Use Number.isNaN() to check for NaN instead.

What is the difference between isNaN and Number.isNaN?

isNaN converts the argument to a number first, so isNaN('hello') returns true because 'hello' becomes NaN. Number.isNaN does not convert, so Number.isNaN('hello') returns false because 'hello' is a string, not NaN.

Conclusion

NaN is JavaScript's way of saying a number operation produced an invalid result. It has two unusual behaviors: typeof NaN is 'number', and NaN is not equal to itself. Use Number.isNaN to check for NaN reliably, and guard your math operations to prevent NaN from spreading through your calculations.