为什么JS中的isNaN(null)== false? [英] Why is isNaN(null) == false in JS?
问题描述
JS中的这段代码给了我一个弹出窗口我认为null是一个数字,我觉得有点令人不安。我缺少什么?
This code in JS gives me a popup saying "i think null is a number", which I find slightly disturbing. What am I missing?
if (isNaN(null)) {
alert("null is not a number");
} else {
alert("i think null is a number");
}
我正在使用Firefox 3.这是浏览器错误吗?
I'm using Firefox 3. Is that a browser bug?
其他测试:
null == NaN; // false
isNaN("text"); // true
NaN == "text" // false
所以,问题似乎不能与NaN精确比较?
So, the problem seems not to be an exact comparison with NaN?
编辑:现在问题已经回答了,我已将我的帖子清理干净了有一个更好的档案版本。然而,这使得一些评论甚至一些答案有点难以理解。不要责怪他们的作者。我更改的内容包括:
Now the question has been answered, I have cleaned up my post to have a better version for the archive. However, this renders some comments and even some answers a little incomprehensible. Don't blame their authors. Among the things I changed was:
- 删除了一条说明我首先通过恢复其含义搞砸了标题的说明
- 早期的答案显示我没有说清楚为什么我认为这种行为很奇怪,所以我添加了检查字符串并进行手动比较的示例。
- Removed a note saying that I had screwed up the headline in the first place by reverting its meaning
- Earlier answers showed that I didn't state clearly enough why I thought the behaviour was weird, so I added the examples that check a string and do a manual comparison.
推荐答案
我相信代码试图问, 是 x
数字?这里的具体情况是 x = null
。函数 isNaN()
可以用来回答这个问题,但从语义上讲,它特指的是值 NaN
。来自维基百科的 NaN
:
I believe the code is trying to ask, "is x
numeric?" with the specific case here of x = null
. The function isNaN()
can be used to answer this question, but semantically it's referring specifically to the value NaN
. From Wikipedia for NaN
:
NaN( N ot a N umber)是一个表示未定义或不可表示的值的数值数据类型的值,尤其是在浮点计算中。
NaN (Not a Number) is a value of the numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations.
在大多数情况下,我们认为答案是否为空数字?应该是没有。但是, isNaN(null)== false
在语义上是正确的,因为 null
不是 NaN
。
In most cases we think the answer to "is null numeric?" should be no. However, isNaN(null) == false
is semantically correct, because null
is not NaN
.
以下是算法说明:
函数 isNaN(x)
尝试将传递的参数转换为数字 1 (相当于 Number(x)
),然后测试该值是否为 NaN
。如果参数无法转换为数字, Number(x)
将返回 NaN
2 。因此,如果将参数 x
转换为数字会导致 NaN
,则返回true;否则,它返回false。
The function isNaN(x)
attempts to convert the passed parameter to a number1 (equivalent to Number(x)
) and then tests if the value is NaN
. If the parameter can't be converted to a number, Number(x)
will return NaN
2. Therefore, if the conversion of parameter x
to a number results in NaN
, it returns true; otherwise, it returns false.
所以在特定情况下 x = null
, null
转换为数字0,(尝试评估 Number(null)
并查看它返回0,)和 isNaN(0)
返回false。只有数字的字符串可以转换为数字,而isNaN也返回false。无法转换为数字的字符串(例如'abcd'
)将导致 isNaN('abcd')
返回true,特别是因为 Number('abcd')
返回 NaN
。
So in the specific case x = null
, null
is converted to the number 0, (try evaluating Number(null)
and see that it returns 0,) and isNaN(0)
returns false. A string that is only digits can be converted to a number and isNaN also returns false. A string (e.g. 'abcd'
) that cannot be converted to a number will cause isNaN('abcd')
to return true, specifically because Number('abcd')
returns NaN
.
除了这些明显的边缘情况之外,返回NaN的标准数值原因如0/0。
In addition to these apparent edge cases are the standard numerical reasons for returning NaN like 0/0.
至于看似不一致的相等测试问题是, NaN
的行为被指定为任何比较 x == NaN
都是假的,不管其他什么操作数,包括 NaN
本身 1 。
As for the seemingly inconsistent tests for equality shown in the question, the behavior of NaN
is specified such that any comparison x == NaN
is false, regardless of the other operand, including NaN
itself1.
这篇关于为什么JS中的isNaN(null)== false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!