混淆isNaN和Number.javascript中的isNaN

Confusion between isNaN and Number.isNaN in javascript

本文关键字:isNaN 中的 javascript 混淆 Number      更新时间:2023-09-26

我对NaN的工作方式感到困惑。我执行了isNaN(undefined),它返回了true。但是如果我用Number.isNaN(undefined),它返回的是false。那么我应该用哪一个呢?

引用一篇关于ES6中数字的文章:

号码。isNaN几乎与ES5全局isNaN方法相同。号码。isNaN返回所提供的值是否等于NaN。这是一个和"这不是一个数字吗?"完全不同的问题。

因此isNaN只是检查传递的值是否不是数字或不能转换为数字。另一方面,Number.isNaN只检查值是否等于NaN(尽管它使用的算法与===不同)。

例如字符串'ponyfoo'不是数字,不能转换为数字,但不是NaN

的例子:

Number.isNaN({});
// <- false, {} is not NaN
Number.isNaN('ponyfoo')
// <- false, 'ponyfoo' is not NaN
Number.isNaN(NaN)
// <- true, NaN is NaN
Number.isNaN('pony'/'foo')
// <- true, 'pony'/'foo' is NaN, NaN is NaN
isNaN({});
// <- true, {} is not a number
isNaN('ponyfoo')
// <- true, 'ponyfoo' is not a number
isNaN(NaN)
// <- true, NaN is not a number
isNaN('pony'/'foo')
// <- true, 'pony'/'foo' is NaN, NaN is not a number

  • isNaN将参数转换为Number,如果结果值为NaN则返回true。
  • Number.isNaN不转换参数;当参数为Number且为NaN时,返回true。

那我该用哪一个呢

我猜你正试图检查值是否看起来像一个数字。在这种情况下,答案是。这些函数检查值是否为IEEE-754 Not A Number。时期。例如:

var your_age = "";
// user forgot to put in their age
if (isNaN(your_age)) {
  alert("Age is invalid. Please enter a valid number.");
} else {
  alert("Your age is " + your_age + ".");
}
// alerts "Your age is ."
// same result when you use Number.isNaN above

这也是为什么结果会有如此大的差异。

如上所述,如果参数不是Number, Number.isNaN将立即返回false,而isNaN首先将值转换为Number。这会改变结果。一些例子:

                |       Number.isNaN()       |        isNaN()
----------------+----------------------------+-----------------------
value           | value is a Number | result | Number(value) | result
----------------+-------------------+--------+---------------+-------
undefined       | false             | false  | NaN           | true
{}              | false             | false  | NaN           | true
"blabla"        | false             | false  | NaN           | true
new Date("!")   | false             | false  | NaN           | true
new Number(0/0) | false             | false  | NaN           | true

我发现,如果你想检查什么是数字(或不是),那么Number.isNaN()Number.parseInt()Number.parseFloat()的组合(取决于你的期望)来覆盖大多数用例:

考虑:测试一堆不同的输入变量对几个数字测试:

r = [NaN, undefined, null, false, true, {}, [], '', ' ', 0, 1, '0', '1']
.map(function(v){return [
    v, 
    isNaN(v), 
    Number.isNaN(v), 
    Number.isInteger(v),
    Number.parseInt(v, 10), 
    Number.isNaN( Number.parseInt(v, 10)) 
];});
console.table(r);
// or if console.table() not available:
r.join(''n', function(v){v.join(',')} );
结果:

NaN      , true , true , false, NaN, true 
undefined, true , false, false, NaN, true 
null     , false, false, false, NaN, true 
false    , false, false, false, NaN, true 
true     , false, false, false, NaN, true 
Object   , true , false, false, NaN, true 
Array(0) , false, false, false, NaN, true 
''       , false, false, false, NaN, true 
' '      , false, false, false, NaN, true 
0        , false, false, true , 0  , false
1        , false, false, true , 1  , false
'0'      , false, false, false, 0  , false
'1'      , false, false, false, 1  , false

注意最后一列,这通常是我的经验。

总之,

isNaN()

将检查值到数字(类型)的转换是否失败

例如

'abc '


Number.isNaN()

将检查给定的值类型是否为数字但不是有效的数字

例如:'bb'/33

首先,忘记值NaN的无用含义,只需将其视为bad number。一看到NaN,就把它读成"坏数字"。

Number.isNaNisNaN的一个更安全和健壮的版本,isNaN有一个bug,

isNaN(NaN) // true, this is the supposed result, the argument is NaN
isNaN('hello') // true, but this is misleading, cuz `hello` is not NaN

isNaN无法将NaN与其他非数字值(如字符)区分。

于是一种新的方法开始发挥作用。

号码。isNaN首先检查值是否为数字,如果不是数字,则立即返回false。

Number.isNaN('hello') // false, 'hello' is not NaN, this is the supposed result

Number.isNaN ()

如果值是Number类型,该方法返回true,等于NaN。否则返回false。例如

Number.isNaN(123) //false
Type of 123 is number but 123 is not equal to NaN, therefore it returns false.

Number.isNaN('123') //false
    
Type of 123 is string & '123' is not equal to NaN, therefore it returns false.

Number.isNaN(NaN) //true
Type of NaN is number & NaN is equal to NaN, therefore it returns true.

Number.isNaN('NaN') //false
Type of 'NaN' is string & 'NaN' is not equal to NaN, therefore it returns false.

global isNaN() function

全局isNaN()函数将测试的值转换为一个Number,然后对其进行测试。

isNaN(123) //false
isNaN('123') //false
isNaN(NaN) //true
isNaN('NaN') //true