NaN means Not-a-Number. It is a special value of JavaScript's Number type used when a numeric operation cannot produce a meaningful numeric result.
const result = "xyz" * 2;
console.log(result); // NaN
Invalid arithmetic and failed numeric conversions are common sources of NaN.
console.log("hello" * 3); // NaN
console.log(Number("hello")); // NaN
console.log(parseFloat("hello")); // NaN
NaN belongs to JavaScript's Number type even though it represents an invalid numeric result.
console.log(typeof NaN); // "number"
Both strict and loose equality comparisons with NaN are false, including comparison with another NaN.
console.log(NaN === NaN); // false
console.log(NaN == NaN); // false
Object.is() is one exception: it treats two NaN values as the same.
console.log(Object.is(NaN, NaN)); // true
For most code, use Number.isNaN() when you need to know whether a value is exactly NaN.
const value = Number("abc");
if (Number.isNaN(value)) {
console.log("Conversion failed");
}
The global isNaN() function performs numeric coercion first, so it answers a slightly different question.
Once NaN enters most arithmetic operations, later results also become NaN. Finding the first failed conversion is therefore important when debugging calculations.
const price = NaN;
console.log(price + 10); // NaN
console.log(price * 2); // NaN
console.log(parseInt("abc", 10)); // NaN
console.log(parseFloat("abc")); // NaNSee parseInt() and parseFloat() for their partial-string parsing rules.
NaN is falsy, but using truthiness alone is not a reliable numeric-validation strategy because 0 is also falsy.
console.log(Boolean(NaN)); // false
console.log(Boolean(0)); // false
Use Number.isNaN() and isNaN() to test values and continue with parseInt() when converting integer text.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.