8

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?

lock
  • 6,404
  • 18
  • 58
  • 76

3 Answers3

37

The NaN value is defined to be unequal to everything, including itself. Test if a value is NaN with the isNaN() function, appropriately enough. (ECMAScript 6 adds a Number.isNan() function with different semantics for non-number arguments, but it's not supported in all browsers yet as of 2015).

There are two built-in properties available with a NaN value: the global NaN property (i.e. window.NaN in browsers), and Number.NaN. It is not a language keyword. In older browsers, the NaN property could be overwritten, with potentially confusing results, but with the ECMAScript 5 standard it was made non-writable.

  • As @some pointed out in the comments, there is also the global function isFinite() which may be useful.
Miles
  • 31,360
  • 7
  • 64
  • 74
  • @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 Feb 18 '09 at 04:08
  • 1
    ohw sorry, wrong maths, thanks again to the very helpful programming community – lock Feb 18 '09 at 04:09
  • 1
    There is also the isFinite function. – some Feb 18 '09 at 04:20
  • 2
    @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 Feb 18 '09 at 07:49
  • 1
    Note that JavaScript's `isNaN(...)` even returns `true` when passing it a non-number, like a primitive string, any object, or `undefined`. If one needs to test if something is a NaN *number*, one can use `a !== a` instead. – Arjan Apr 14 '13 at 20:17
  • NaN = 1; NaN === 1; // false, even though NaN === NaN is true at this point – jk7 May 30 '15 at 23:18
  • @jk7: You can't overwrite `NaN` (or `undefined`) in most browsers anymore, since this answer was originally written—see update – Miles May 30 '15 at 23:37
  • @Miles: I agree with your answer. Just exploring how NaN works based the discussion in the comments. Oddly enough, setting NaN=1 and then testing NaN === NaN now returns false when it had returned true yesterday - same version of Firefox, same test code. – jk7 May 31 '15 at 21:29
2

the best way to check the result of numeric operation against NaN is to aproach this way , example:

var x  = 0;
var y  = 0;
var result = x/y;  // we know that result will be NaN value
// to test if result holds a NaN value we should use the following code :

if(result !=result){
console.log('this is an NaN value');
} 

and it's done.

the trick is that NaN can't be compared to any other value even with it self(NaN !=NaN is always true so we can take advantage of this and compare result against itself)

this is JavaScript(a good and bizarre language)

ismnoiet
  • 4,129
  • 24
  • 30
1

Equality operator (== and ===) cannot be used to test a value against NaN. Use Number.isNaN() or isNaN() instead.

NaN === NaN;        // false
Number.NaN === NaN; // false
isNaN(NaN);         // true
isNaN(Number.NaN);  // true
Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69