只是出于好奇。

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

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

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

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


当前回答

NaN是一个有效的浮点值(http://en.wikipedia.org/wiki/NaN)

NaN === NaN为假,因为它们不一定是相同的非数字

其他回答

一个例子

假设我们正在将一个字符串转换为一个数字:

Number("string"); // returns NaN

我们将数据类型更改为数字,但它的值不是数字!

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

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

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

Javascript只有一种数字数据类型,即标准的64位双精度浮点数。所有东西都是双的。NaN是double的特殊值,但它仍然是double。

所有parseInt所做的是将字符串“转换”为数值数据类型,因此结果总是“number”;只有当原始字符串不可解析时,它的值才会是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.