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?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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 aNumber.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), andNumber.NaN
. It is not a language keyword. In older browsers, theNaN
property could be overwritten, with potentially confusing results, but with the ECMAScript 5 standard it was made non-writable.the best way to check the result of numeric operation against NaN is to aproach this way , example:
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)
Equality operator (== and ===) cannot be used to test a value against NaN. Use Number.isNaN() or isNaN() instead.