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.
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.
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:
console.log("abc" * 2); // NaN
console.log("abc" / 2); // NaN
console.log("abc" - 2); // NaNNotice 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:
console.log(parseInt("xyz")); // NaN
console.log(parseFloat("abc")); // NaNMath 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:
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:
console.log(Math.sqrt(-1)); // NaNNaN 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.
console.log(NaN === NaN); // false
console.log(NaN == NaN); // falseThis 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:
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)); // falseThe 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:
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:
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); // NaNBecause 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
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.
Frequently Asked Questions
Why does typeof NaN return 'number'?
Why is NaN === NaN false?
What is the difference between isNaN and Number.isNaN?
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.
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.