tags:

views:

770

answers:

1

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?

+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 (like undefined, but unlike the keyword this or the literals true, false, and null).
  • As "some" pointed out in the comments, there is also the global function isFinite() which may be useful.
Miles
thanks a lot :) really helped +20rep for you
lock
@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
ohw sorry, wrong maths, thanks again to the very helpful programming community
lock
There is also the isFinite function.
some
NaN = 1; NaN===NaN; //true
Breton
@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