IsNAN false // should be true!
Not necessarily. :-) isNaN
is only true if the argument to it (converted to a Number
if necesssary) is NaN
.
Depending on what the object is, it may or may not be NaN
when converted to a Number
. It depends on what happens when the object is converted to its primitive value. null
, for instance, converts to 0
. In many, many other cases conversion to primitive involves turning it into a string (arrays, for instance), and the default toString
of some objects may return something that can be converted to a non-NaN
Number
.
Examples of non-NaN
objects:
// null
console.log(Number(null)); // 0
// an array (which becomes "23" when converted to primitive, because
// arrays do `join` for `toString`; then the "23" becomes 23)
console.log(Number([23])); // 23
// another object implementing toString or valueOf in a way that returns
// something convertable to a non-NaN number
console.log(Number(new Foo("47"))); // 47
...where Foo
is:
function Foo(value) {
this.value = value;
}
Foo.prototype.toString = function() {
return this.value;
};
(In the above, toString
could be valueOf
as well.)