Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...