Is Number.IsNaN() more broken than isNaN()
isNaN()
and Number.isNaN()
both test if a value is (or, in the case of isNaN()
, can be converted to a number-type value that represents) the NaN
value. In other words, "NaN" does not simply mean "this value is not a number", it specifically means "this value is a numeric Not-a-Number value according to IEEE-754".
The reason all your tests above return false is because all of the given values can be converted to a numeric value that is not NaN
:
Number('') // 0
Number(' ') // 0
Number(true) // 1
Number(false) // 0
Number([0]) // 0
The reason isNaN()
is "broken" is because, ostensibly, type conversions aren't supposed to happen when testing values. That is the issue Number.isNaN()
is designed to address. In particular, Number.isNaN()
will only attempt to compare a value to NaN
if the value is a number-type value. Any other type will return false, even if they are literally "not a number", because the type of the value NaN
is number. See the respective MDN docs for isNaN()
and Number.isNaN()
.
If you simply want to determine whether or not a value is of the number type, even if that value is NaN
, use typeof
instead:
typeof 'RAWRRR' === 'number' // false
The key difference between the two is that the global isNaN(x)
function performs a conversion of the parameter x
to a number. So
isNaN("blabla") === true
because Number("blabla")
results in NaN
There are two definitions of "not a number" here and that's perhaps where the confusion lies. Number.isNaN(x)
only returns true for the IEEE 754 floating point specification's definition of Not a Number, for example:
Number.isNaN(Math.sqrt(-1))
as opposed to determining whether the object being passed in is of numeric type or not. Some ways of doing that are:
typeof x === "number"
x === +x
Object.prototype.toString.call(x) === "[object Number]"
As mentioned in a comment isNaN()
and Number.isNaN()
both check that the value you pass in is not equal to the value NaN
. The key here is that NaN
is an actual value and not an evaluated result e.g. "blabla"
is a String
and the value is "blabla"
which means it is not the value "NaN"
.
A plausible solution would be doing something like:
Number.isNaN(Number("blabla")); //returns true.
No, the original isNaN
is broken. You are not understanding the point of isNaN
.
The purpose of both of these functions is to determine whether or not something has the value NaN
. This is provided because something === NaN
will always be false
and therefore can't be used to test this.
(side note: something !== something
is actually a reliable, although counter-intuitive, test for NaN
)
The reason isNaN
is broken is that it can return true
in cases when a value is not actually NaN
. This is because it first coerces the value to a number.
So
isNaN("hello")
is true
, even though "hello"
is not NaN
.
If you want to check whether a value actually is a finite number, you can use:
Number.isFinite(value)
If you want to test whether a value is a finite number or a string representation of one, you can use:
Number.isFinite(value) || (Number.isFinite(Number(value)) && typeof value === 'string')