Hiprup

What is the output of typeof NaN?

typeof NaN returns "number". Despite meaning "Not a Number", NaN is a special numeric value, so its type is number.

  • Where it comes from — invalid math like 0/0 or Number("abc").

  • Unique quirk — NaN is the only value not equal to itself, so NaN === NaN is false.

Check it with Number.isNaN(value) — the reliable, type-safe way.

console.log(typeof NaN);          // 'number' (surprising!)
console.log(NaN === NaN);          // false (only value !== itself)
console.log(NaN !== NaN);          // true

// What produces NaN
console.log(0 / 0);               // NaN
console.log(parseInt('abc'));      // NaN
console.log(Math.sqrt(-1));        // NaN
console.log(undefined + 1);        // NaN
console.log(Number('hello'));      // NaN

// Checking for NaN
console.log(Number.isNaN(NaN));    // true (correct)
console.log(Number.isNaN('hello')); // false (correct — string, not NaN)

console.log(isNaN(NaN));           // true
console.log(isNaN('hello'));       // true (WRONG — coerces to NaN first)

// Self-inequality trick
function isNaNCheck(value) {
  return value !== value; // Only true for NaN!
}

typeof NaN is 'number' because NaN is a numeric type (IEEE 754 special value). NaN !== NaN is the only self-inequality in JS. Number.isNaN checks without coercion (correct).

Global isNaN coerces first, giving false positives. The self-inequality trick (value !== value) only returns true for NaN.

typeof NaN === 'number' is the surprise. NaN !== NaN is the unique behavior.

Always use Number.isNaN (not global isNaN). The self-inequality trick (value !== value) is an impressive alternative NaN check.

What is the output of typeof NaN? | Hiprup