为什么IsNaN(x)与x == NaN不同,其中x = NaN [英] Why is IsNaN(x) different from x == NaN where x = NaN

查看:219
本文介绍了为什么IsNaN(x)与x == NaN不同,其中x = NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这两个不同?

var x = NaN; //e.g. Number("e");
alert(isNaN(x)); //true (good)
alert(x == NaN); //false (bad)

推荐答案

没有等于NaN.任何比较都将始终为false.

Nothing is equal to NaN. Any comparison will always be false.

在严格和抽象比较算法中,如果类型相同,并且任何一个操作数为NaN,则结果将为false.

In both the strict and abstract comparison algorithms, if the types are the same, and either operand is NaN, the result will be false.

如果Type(x)为数字,则

If Type(x) is Number, then

  • 如果xNaN,则返回false.
  • 如果yNaN,则返回false.
  • If x is NaN, return false.
  • If y is NaN, return false.

在抽象算法中,如果类型不同,并且NaN是操作数之一,则另一个操作数最终将被强制为数字,并带我们回到上面的情况.

In the abstract algorithm, if the types are different, and a NaN is one of the operands, then the other operand will ultimately be coerced to a number, and will bring us back to the scenario above.

这篇关于为什么IsNaN(x)与x == NaN不同,其中x = NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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