Javascript变量/'NaN'表现得很奇怪

时间:2012-04-10 06:11:28

标签: javascript nan

我有几个文本框,如果有效数据在文本框中,则会返回javascript Number值,否则为NaN。我得到了这种奇怪的行为。当我签入firebug时(两个文本框都是空白的):

>>> hours
NaN
>>> minutes
NaN
>>> minutes == NaN
false
>>> hours == NaN
false
>>> hours == minutes
false

为什么表现如此?

1 个答案:

答案 0 :(得分:3)

NaN不等于任何内容,甚至不是NaN

Reference at MDN

More detailed SO question and answer

对于权威来源,请参阅ECMAScript 5 Official Specification11.9.1部分和11.9.3部分:

1. If Type(x) is the same as Type(y), then
     [...]
  c. If Type(x) is Number, then
     i. If x is NaN, return false.
    ii. If y is NaN, return false.
        [...]