JavaScript traps · JavaScript

JavaScript: typeof NaN = "number"?!PRO

NaN is a special value of the number type: the answer to bad maths. Test for it with Number.isNaN.

// typeof NaN = "number" ?!
// NaN ("Not a Number") is a special VALUE of the number type: the result of bad maths.
console.log(typeof NaN);          // number ?!
console.log(0 / 0, Math.sqrt(-1)); // NaN NaN: both are numbers that went wrong

// Fix: test for it with Number.isNaN
console.log(Number.isNaN(NaN));   // true ✓
console.log(NaN === NaN);         // false: never compare with ===
Open code JS11 in Pocket Compiler Download the Pocket Compiler app — free

No app yet? Install it, then open the app and type the code JS11 in Code Library.