5 ms·
Yet another post that underestimates the importance on NaN, it isn't 'WTF' at all if you understand its design. Sure, the naming in ES makes it a bit silly sinc
by jensnockert 14y ago
Yet another post that underestimates the importance on NaN, it isn't 'WTF' at all if you understand its design. Sure, the naming in ES makes it a bit silly since NaN is a number, but it is something you only have to see once to understand.
Don't mess with IEEE 754, it is almost universally adopted for binary floating-point for very good reasons.
- elliptic 14y agoNaN is incredibly useful, but I'm not so sure that 1/"a" = NaN is a great idea.
- grayrest 14y agoStrings that arent numeric are type coerced into NaN. Using unary plus to coerce to number: +"2" is the primitive number 2 while +"a" is NaN and per the floating point spec, any arithmetic operation on NaN results in NaN. The correct though unintuitive way to check for NaN is `(x === x) === false`. NaN is a number per the IEEE 754. The majority of Javascript WTFs are due to type coercion. Use triple equals and check your inputs to avoid.
- some-1 14y agoNo, the correct way is to use the global function isNaN(x);
- grayrest 14y agoisNaN will return true for NaN and all values that coerce to it. The equals check will test ONLY for NaN itself: var x = "a"; isNaN(x); // true (x === x) === false // false x = NaN isNaN(x); // true (x === x) === false // true