The isNaN()
function implicitly coerces its argument to be a number. If you pass it the string "12", it returns false
. Thus in your code the value of the variable could be a string representation of a number, and it'll make it through the tests if it's a "nice" string.
The concept of NaN
does not really mean, generally, "not a number". It's not really about data types. It's a feature of the IEEE floating point format. There are bit patterns that are "not numbers", and the pseudo-value NaN
represents those. It happens that the language designers decided to have failed numeric conversions result in NaN
as a sort of marker.
In newer JavaScript environments, there's another isNaN
function on the Number
constructor. It differs from the global isNaN()
in that it performs no type coercion at all, and it only checks to see if its argument is NaN
. If you pass it a string — even a string like "banana", which isn't anything like a number — it returns false
, because no string value can be the number NaN
.