Node.js中的Number.isNaN('asd')===false!什么给予

Number.isNaN('asd') === false in Node.js! What gives?

本文关键字:false 什么 js Number 中的 Node asd isNaN      更新时间:2023-09-26

试试:在Node.js或Firefox中,打开一个REPL,然后键入:

Number.isNaN('asdf');

这怎么可能不是NaN?如果不是这个,NaN是什么?

您有一个误解。NaN的意思是不是数字,这并不意味着任何不是数字的东西都是NaN

NaN是浮点运算中的一个特殊值,表示运算的未定义结果。例如,作为结果,0 / 0通常产生NaN

更多信息请点击此处。

语言规范中没有Number.isNaN()的概念。唯一指定的函数是全局isNaN(15.1.2.4),它将在任何不是数字或无法正确转换为数字的情况下正确返回true

Number.isNaN(在NaN上返回true)是,可能是下一个标准草案的实现细节部分(参见remyabel的回答),isNaN可以在内部使用。

请注意,Number.IsNaN的支持似乎有限。

此输出:

console.log(Number.isNaN("blabla"));

给出falsefalse !== NaN:

console.log(false !== NaN)
console.log(false !== Number.NaN);

这里有一个参考:

当使用一个参数编号调用Number.isNaN时采取以下步骤:

If Type(number) is not Number, return false.
If number is NaN, return true.
Otherwise, return false.

此函数与全局isNaN函数(18.2.3)的不同之处在于在确定之前,它不会将其参数转换为数字无论是NaN。

我是l33t,因为我有1337的声誉

试试这个:

function checkNaN(value)
{
    return value !== value;
}

它之所以有效,是因为(我相信)NaN是JS中唯一一个不严格等于自身的东西。

这是EcmaScript 6标准的一部分。

Number.isNaN在不强制参数的情况下返回值是否为NaN。

Number.isNaN("xmfds"); // false
Number.isNaN("ose"); // false

isNaN在将参数强制为Number类型后返回值是否为NaN

isNaN("ose") -> isNaN( Number("ose") ) -> isNaN(NaN); // true