javascript 中 NaN 的保留关键字是什么?

发布于 2024-07-13 23:48:14 字数 223 浏览 7 评论 0原文

如果我想测试表达式的结果并且该函数将返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凝望流年 2024-07-20 23:48:14

NaN 值被定义为不等于一切,包括它本身。 使用 isNaN 测试值是否为 NaN ()函数,恰如其分。 (ECMAScript 6 添加了一个 Number。 isNan() 函数对于非数字参数具有不同的语义,但截至 2015 年,并非所有浏览器都支持它。

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

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

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.
别靠近我心 2024-07-20 23:48:14

检查 NaN 数值运算结果的最佳方法是采用这种方式,
示例:

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');
} 

就这样完成了。

诀窍在于 NaN 不能与任何其他值进行比较,即使它本身也是如此(NaN !=NaN 始终为真,因此我们可以利用这一点并将结果与​​其自身进行比较)

这是 JavaScript(一个很好但奇怪的东西)语言)

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)

梦冥 2024-07-20 23:48:14

等号运算符(== 和 ===)不能用于测试 NaN 值。 请改用 Number.isNaN() 或 isNaN()。

NaN === NaN;        // false
Number.NaN === NaN; // false
isNaN(NaN);         // true
isNaN(Number.NaN);  // true

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文