javascript中NaN的保留关键字是什么? [英] what is the reserved keyword for NaN in javascript?

查看:203
本文介绍了javascript中NaN的保留关键字是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想测试表达式的结果并且该函数将返回NaN
我将如何检查?

示例:
$(' amount')。value.toInt()!='NaN'
^不起作用,我假设返回的值不是字符串,

$('amount')。value.toInt()!= NaN
^似乎也没有工作,这个看起来很明显

b $ b
so我如何检查返回值是否为数字?

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?

推荐答案

NaN值定义为不等于所有内容,包括本身。使用 <$ c $测试值是否为NaN c> isNaN() 功能,足够恰当。 (ECMAScript 6添加了 Number.isNan() 函数具有非数字参数的不同语义,但从2015年起在所有浏览器中都不支持。)

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).

有两个内置属性可用,具有NaN值:全局 NaN 属性(即浏览器中的 window.NaN ),以及 Number.NaN 。它不是 语言关键字。在旧版浏览器中, NaN 属性可能会被覆盖,结果可能会令人困惑,但 ECMAScript 5标准它是不可写的

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.


  • 正如@some在评论中指出的那样,还有全局函数 isFinite()可能有用。

  • As @some pointed out in the comments, there is also the global function isFinite() which may be useful.

这篇关于javascript中NaN的保留关键字是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆