Javascript Nan
// Problem with NaN:
The NaN property represents a value that is “not a number”. This special value
results from an operation that could not be performed either because one of the
operands was non-numeric (e.g., "abc" / 4), or because the result of the
operation is non-numeric (e.g., an attempt to divide by zero).
Although NaN means “not a number”, its type is, believe it or not is 'number'.
NaN compared to anything – even itself! – is false.
A semi-reliable way to test whether a number is equal to NaN is with the
built-in function isNaN(), but even using isNaN() is an imperfect solution.
Dividing zero by zero results in a NaN — but dividing other numbers by zero does
not.
isNaN(NaN); // true
isNaN(undefined); // true
isNaN({}); // true
isNaN(true); // false
isNaN(null); // false
isNaN(37); // false
isNaN("37"); // false: "37" is converted to the number 37 which is not NaN
isNaN("37.37"); // false: "37.37" is converted to the number 37.37 which is not NaN
isNaN("123ABC"); // true: parseInt("123ABC") is 123 but Number("123ABC") is NaN
isNaN(""); // false: the empty string is converted to 0 which is not NaN
isNaN(" "); // false: a string with spaces is converted to 0 which is not NaN
// dates
isNaN(new Date()); // false
isNaN(new Date().toString()); // true
// This is a false positive and the reason why isNaN is not entirely reliable
isNaN("blabla") // true: "blabla" is converted to a number.
// Parsing this as a number fails and returns NaN
ES6 offers a new Number.isNaN() function, which is a different and more reliable
than the old global isNaN() function.
A better solution would either be to use value !== value, which would only
produce true if the value is equal to NaN.
page revision: 0, last edited: 14 Nov 2016 04:52