JavaScript traps · JavaScript

JavaScript: NaN === NaN is FALSE?!PRO

Watch the video

NaN never equals anything, not even itself. Number.isNaN() vs the old isNaN().

// NaN === NaN is FALSE ?! — NaN never equals anything, not even itself.
const x = Number("hello");     // NaN
console.log(x === NaN);        // false ?!
console.log(Number.isNaN(x));  // true ✓
console.log(Object.is(x, NaN));// true ✓

// Bonus trap: the old global isNaN converts first
console.log(isNaN("hello"));        // true  (string → NaN)
console.log(Number.isNaN("hello")); // false (it's a string, not NaN)
Open code JS3 in Pocket Compiler Download the Pocket Compiler app — free

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