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
NaNisNumber, NaNdoes mean “Not a number”, which is not the same thing as “not of typeNumber“,0/0is a good example ofNaN: it is still a number, but JavaScript (and nobody else) can say what is the real value of zero divided by zero.1/0on the other hand returnsInfinity, which is notNaN.
As I understand it,
NaNis a sentinel instance of theNumberclass that represents, well, exactly what it stands for – numeric results that cannot be adequately represented. So0/0is not a number, in the sense that it’sNaN, but it is aNumberin terms of its type.Perhaps it should have been called NaRN (Not a Representable Number).