只是出于好奇。

typeof NaN是number似乎不太符合逻辑。就像NaN === NaN或NaN == NaN返回false。这是JavaScript的特性之一吗,或者有什么原因吗?

编辑:谢谢你的回答。然而,让人明白这不是一件容易的事情。阅读答案和维基我明白了更多,但仍然,一句话像

与NaN的比较总是返回无序结果,即使是与NaN本身比较也是如此。比较谓词是信号或非信号,信号版本表示此类比较的无效异常。等式和不等式谓词是非信号,因此x = x返回false可用于测试x是否为静态NaN。

让我头晕目眩。如果有人能把它翻译成人类(而不是数学家)可读的语言,我会很感激。


当前回答

typeof NaN返回'number',因为:

ECMAScript spec says the Number type includes NaN: 4.3.20 Number type set of all possible Number values including the special “Not-a-Number” (NaN) values, positive infinity, and negative infinity So typeof returns accordingly: 11.4.3 The typeof Operator The production UnaryExpression : typeof UnaryExpression is evaluated as follows: Let val be the result of evaluating UnaryExpression. If Type(val) is Reference, then If IsUnresolvableReference(val) is true, return "undefined". Let val be GetValue(val). Return a String determined by Type(val) according to Table 20. ​ Table 20 — typeof Operator Results ================================================================== | Type of val | Result | ================================================================== | Undefined | "undefined" | |----------------------------------------------------------------| | Null | "object" | |----------------------------------------------------------------| | Boolean | "boolean" | |----------------------------------------------------------------| | Number | "number" | |----------------------------------------------------------------| | String | "string" | |----------------------------------------------------------------| | Object (native and does | "object" | | not implement [[Call]]) | | |----------------------------------------------------------------| | Object (native or host and | "function" | | does implement [[Call]]) | | |----------------------------------------------------------------| | Object (host and does not | Implementation-defined except may | | implement [[Call]]) | not be "undefined", "boolean", | | | "number", or "string". | ------------------------------------------------------------------

此行为符合IEEE浮点运算标准(IEEE 754):

4.3.19 Number value primitive value corresponding to a double-precision 64-bit binary format IEEE 754 value 4.3.23 NaN number value that is a IEEE 754 “Not-a-Number” value 8.5 The Number Type The Number type has exactly 18437736874454810627 (that is, 253−264+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic, except that the 9007199254740990 (that is, 253−2) distinct “Not-a-Number” values of the IEEE Standard are represented in ECMAScript as a single special NaN value. (Note that the NaN value is produced by the program expression NaN.)

其他回答

从类型的角度来看,NaN是一个数字,但不是像1、2或329131这样的普通数字。“不是一个数字”的名字指的是表示的值是特殊的,是关于IEEE格式规范领域,而不是javascript语言领域。

如果使用jQuery,我更喜欢isNumeric检查类型:

console.log($.isNumeric(NaN));  // returns false
console.log($.type(NaN));       // returns number

http://api.jquery.com/jQuery.isNumeric/

typeof NaN返回'number',因为:

ECMAScript spec says the Number type includes NaN: 4.3.20 Number type set of all possible Number values including the special “Not-a-Number” (NaN) values, positive infinity, and negative infinity So typeof returns accordingly: 11.4.3 The typeof Operator The production UnaryExpression : typeof UnaryExpression is evaluated as follows: Let val be the result of evaluating UnaryExpression. If Type(val) is Reference, then If IsUnresolvableReference(val) is true, return "undefined". Let val be GetValue(val). Return a String determined by Type(val) according to Table 20. ​ Table 20 — typeof Operator Results ================================================================== | Type of val | Result | ================================================================== | Undefined | "undefined" | |----------------------------------------------------------------| | Null | "object" | |----------------------------------------------------------------| | Boolean | "boolean" | |----------------------------------------------------------------| | Number | "number" | |----------------------------------------------------------------| | String | "string" | |----------------------------------------------------------------| | Object (native and does | "object" | | not implement [[Call]]) | | |----------------------------------------------------------------| | Object (native or host and | "function" | | does implement [[Call]]) | | |----------------------------------------------------------------| | Object (host and does not | Implementation-defined except may | | implement [[Call]]) | not be "undefined", "boolean", | | | "number", or "string". | ------------------------------------------------------------------

此行为符合IEEE浮点运算标准(IEEE 754):

4.3.19 Number value primitive value corresponding to a double-precision 64-bit binary format IEEE 754 value 4.3.23 NaN number value that is a IEEE 754 “Not-a-Number” value 8.5 The Number Type The Number type has exactly 18437736874454810627 (that is, 253−264+3) values, representing the double-precision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic, except that the 9007199254740990 (that is, 253−2) distinct “Not-a-Number” values of the IEEE Standard are represented in ECMAScript as a single special NaN value. (Note that the NaN value is produced by the program expression NaN.)

这只是因为NaN是JS中Number对象的一个属性,它与它是否是一个数字无关。

NaN值实际上是Number。因此,当你问它是否是一个数字时,它会说是的。使用isNaN()调用是正确的。

作为信息,NaN也可以通过对未定义的数字(如除零或负数的平方根)的操作返回。