javascript 中 NaN 的保留关键字是什么?
如果我想测试表达式的结果并且该函数将返回 NaN 我该如何检查?
示例:$('amount').value.toInt()!='NaN'
^ 不起作用,我假设返回的值不是字符串,$('amount').value.toInt()!=NaN
^ 似乎也不起作用,而且这个似乎很明显
那么如何检查返回值是否不是数字呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NaN 值被定义为不等于一切,包括它本身。 使用
isNaN 测试值是否为 NaN ()
函数,恰如其分。 (ECMAScript 6 添加了一个Number。 isNan()
函数对于非数字参数具有不同的语义,但截至 2015 年,并非所有浏览器都支持它。有两个可用 NaN 值的内置属性:全局
NaN
属性(即浏览器中的window.NaN
),以及Number.NaN
。 它不是语言关键字。 在较旧的浏览器中,NaN
属性可能会被覆盖,可能会产生令人困惑的结果,但使用 ECMAScript 5 标准使其不可写。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.检查 NaN 数值运算结果的最佳方法是采用这种方式,
示例:
就这样完成了。
诀窍在于 NaN 不能与任何其他值进行比较,即使它本身也是如此(NaN !=NaN 始终为真,因此我们可以利用这一点并将结果与其自身进行比较)
这是 JavaScript(一个很好但奇怪的东西)语言)
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)
等号运算符(== 和 ===)不能用于测试 NaN 值。 请改用 Number.isNaN() 或 isNaN()。
Equality operator (== and ===) cannot be used to test a value against NaN. Use Number.isNaN() or isNaN() instead.