Javascript has two separate functions to test whether the value being tested is a Number or not.
isNaN()
The first one is a global isNan() function, which strictly tests whether the test value is a Number or not. So if you pass a string to this function, it will return true. For example, the following will all return true (i.e. the following arguments are all strictly Not A Number)
isNaN('Hello') //true
isNaN('2005/12/12') //true
isNaN(undefined) //true
isNaN('NaN') //true
isNaN(NaN) //true
isNaN(0 / 0) //true
The global isNaN() function, converts the tested value to a Number, then tests it. This may not be the behavior you are looking for, though.
Number.isNaN()
Number.isNan() does not convert the values to a Number, and will not return true for any value that is not of the type Number. So it has the following behavior:
Number.isNaN(123) //false
Number.isNaN(-1.23) //false
Number.isNaN(5-2) //false
Number.isNaN(0) //false
Number.isNaN('123') //false
Number.isNaN('Hello') //false
Number.isNaN('2005/12/12') //false
Number.isNaN('') //false
Number.isNaN(true) //false
Number.isNaN(undefined) //false
Number.isNaN('NaN') //false
Number.isNaN(NaN) //true
Number.isNaN(0 / 0) //true
Leave a comment