Why JavaScript says that a number is not a number?

Arseni Mourzenko picture Arseni Mourzenko · Jul 9, 2010 · Viewed 38.6k times · Source

I have a piece of JavaScript code which is expected to set an integer value to a variable.

Something is broken, so when I try to do alert(A);, it returns NaN. isNaN(A); returns true. But if I alert(typeof(A));, it says number.

So how can a variable be a number and not a number at the same time? Maybe I misunderstood what NaN really is?


Edit: thanks to the answers, I see that I was wrong, because:

  • The type of NaN is Number,
  • NaN does mean "Not a number", which is not the same thing as "not of type Number",
  • 0/0 is a good example of NaN: it is still a number, but JavaScript (and nobody else) can say what is the real value of zero divided by zero. 1/0 on the other hand returns Infinity, which is not NaN.

Answer

Andrzej Doyle picture Andrzej Doyle · Jul 9, 2010

As I understand it, NaN is a sentinel instance of the Number class that represents, well, exactly what it stands for - numeric results that cannot be adequately represented. So 0/0 is not a number, in the sense that it's NaN, but it is a Number in terms of its type.

Perhaps it should have been called NaRN (Not a Representable Number).