JavaScript NaN (Not-a-Number)

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

How NaN is produced Top ↑

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

Why typeof NaN is "number" Top ↑

NaN belongs to JavaScript's Number type even though it represents an invalid numeric result.

console.log(typeof NaN); // "number"

NaN is not equal to itself Top ↑

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

How to check for NaN Top ↑

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.

How NaN propagates through calculations Top ↑

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

NaN from parseInt() and parseFloat() Top ↑

console.log(parseInt("abc", 10)); // NaN
console.log(parseFloat("abc"));   // NaN

See parseInt() and parseFloat() for their partial-string parsing rules.

NaN in Boolean conditions Top ↑

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.

JavaScript Math Reference




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer