Number.isNaN question

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??

1 Like

Hi @RhoadsNRoses82,

This question is actually NOT a noob question. It’s something that even some experienced javascript developers miss out on. And believe it or not, such questions are asked in interviews to check the depth of your knowledge of the language.

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

Notice that isNaN considers the string “123” as a valid number too.

Javascript usually gets a lot of slack for having such complicated behaviour. But that’s how it’s built, so we have got to live with it. :laughing:

Hope this clears your not so noob question (infact a pro question ! ) :smile:

Happy Learning ! :slight_smile:


interesting! So it sounds like it just is what it is? I am seeing several outcomes there that dont’ make sense to me aside from the ‘123’. For example, isNaN(null) is false, meanwhile isNaN(undefined) is true. It seemed to me that null and undefined were qualitatively similar in javascript. Also, isNaN(5/0) is false, while isNaN(0/0) is true. I guess i will have to do as you say, and learn to live with it! thank you for the detailed explanation