if i want to test the result of an expression and the function would return NaN
how would i check that?
examples: $('amount').value.toInt()!='NaN'
^ does not work and i assume that the returned value is not a string,
$('amount').value.toInt()!=NaN
^ doesnt seem to work either and this one seems obvious
so how do i check wether the returned value is not a number?
views:
770answers:
1
+23
A:
Test if a value is NaN with the isNaN() function, appropriately enough. NaN is defined to be unequal to everything, including itself.
For the sake of completeness:
NaN
is not a keyword, but is rather a built-in property of the global object, and as such may be replaced (likeundefined
, but unlike the keywordthis
or the literalstrue
,false
, andnull
).- As "some" pointed out in the comments, there is also the global function isFinite() which may be useful.
Miles
2009-02-18 04:04:13
thanks a lot :) really helped +20rep for you
lock
2009-02-18 04:05:43
@lock, it's 10 for a vote and 15 for an accept, so that's 25 (unless things have changed recently). And here's another 10, @Miles.
paxdiablo
2009-02-18 04:08:08
ohw sorry, wrong maths, thanks again to the very helpful programming community
lock
2009-02-18 04:09:14
There is also the isFinite function.
some
2009-02-18 04:20:12
NaN = 1; NaN===NaN; //true
Breton
2009-02-18 04:24:38
@Breton: NaN=1; alert(isNaN(NaN)); // alerts false. but isNaN("text") still returns true. It's the same with undefined. That too is just a global variable with the primitive value undefined. You can do undefined=1. Thats why one use: typeof variable === "undefined"
some
2009-02-18 07:49:09