只是出于好奇。

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

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

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

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


当前回答

NaN代表Not a Number。它是数值型数据类型(通常是浮点型,但不总是)的值,表示无效操作(如除零)的结果。

尽管它的名称表明它不是数字,但用于保存它的数据类型是数字类型。因此在JavaScript中,请求NaN的数据类型将返回number(正如alert(typeof(NaN))清楚地演示的那样)。

其他回答

它是Number类型的特殊值,如POSITIVE_INFINITY

为什么?通过设计

NaN仍然是数字类型,但它表示不能表示有效数字的值。

因为NaN是数值型数据类型。

ECMAScript (JavaScript)标准指定数字为IEEE 754浮点数,其中包括NaN作为可能的值。

ECMA 262 5e Section 4.3.19:数字值 原语值对应于双精度64位二进制格式IEEE 754值。

ECMA 262 5e Section 4.3.23: NaN 数字值,它是IEEE 754“非数字”值。

维基百科上的IEEE 754 IEEE浮点算术标准是由电气和电子工程师协会建立的技术标准,也是浮点计算最广泛使用的标准[…] 标准定义了 算术格式:二进制和十进制浮点数据集,由有限数(包括有符号的零和次正规数)、无穷大和特殊的“非数字”值(nan)组成 […]

Javascript uses NaN to represent anything it encounters that can't be represented any other way by its specifications. It does not mean it is not a number. It's just the easiest way to describe the encounter. NaN means that it or an object that refers to it could not be represented in any other way by javascript. For all practical purposes, it is 'unknown'. Being 'unknown' it cannot tell you what it is nor even if it is itself. It is not even the object it is assigned to. It can only tell you what it is not, and not-ness or nothingness can only be described mathematically in a programming language. Since mathematics is about numbers, javascript represents nothingness as NaN. That doesn't mean it's not a number. It means we can't read it any other way that makes sense. That's why it can't even equal itself. Because it doesn't.