Just out of curiosity.
It doesn't seem very logical that typeof NaN
is number. Just like NaN === NaN
or NaN == NaN
returning false, by the way. Is this one of the peculiarities of javascript, or would there be a reason for this?
Edit: thanks for your answers. It's not an easy thing to get ones head around though. Reading answers and the wiki I understood more, but still, a sentence like
A comparison with a NaN always returns an unordered result even when comparing with itself. The comparison predicates are either signaling or non-signaling, the signaling versions signal an invalid exception for such comparisons. The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN.
just keeps my head spinning. If someone can translate this in human (as opposed to, say, mathematician) readable language, I would be gratefull.
This is simply because
NaN
is a property of the Number object in JS, It has nothing to do with it being a number.If using jQuery, I prefer
isNumeric
over checking the type:http://api.jquery.com/jQuery.isNumeric/