Total n00b here - but as i understood from the reading, Number.isNaN should return true if the argument given is NaN. I tried doing Number.isNaN(foot) and it returned false. I then tried Number.isNaN(5/0) and it still returns false. What am i doing wrong??
Now what is happening here is pretty weird but nevertheless interesting.
There are two functions called isNaN –
Number.isNaN() // This is a function property of the "Number" Object
isNaN() // This is a global function
These two behave differently.
The first one only checks for NaN type arguments. i.e only if you provide NaN , it returns true
The second one loosely checks if it’s an invalid number.
Example of these two are below –
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
isNaN(123) //false isNaN(-1.23) //false isNaN(5-2) //false isNaN(0) //false isNaN('123') //false isNaN('Hello') //true isNaN('2005/12/12') //true isNaN('') //false isNaN(true) //false isNaN(undefined) //true isNaN('NaN') //true isNaN(NaN) //true isNaN(0 / 0) //true isNaN(null) //false
isNaN considers the string “123” as a valid number too.
Hope this clears your not so noob question (infact a pro question ! )
Happy Learning !