为什么 Number.isNaN() return 对字符串是假的?
Why does Number.isNaN() return false for Strings?
据我理解,NaN
代表Not A Number
。 Strings 不一定是 Numbers 因此我希望下面的代码对字符串 return true
。然而,事实并非如此。
console.log(Number.isNaN("Stack Overflow"));
有人可以澄清一下吗?
不是Number.isNaN。
console.log(isNaN("Stack Overflow"));
试试看:
console.log(isNaN("Stack Overflow"));
根据MDN,非数字值首先被强制转换为数字,然后测试是否为 NaN。所以,空字符串(""
)在JS中被认为是假的,因此被转换为0,这肯定是一个数字。
In comparison to the global isNaN() function, Number.isNaN() doesn't
suffer the problem of forcefully converting the parameter to a number.
This means it is now safe to pass values that would normally convert
to NaN, but aren't actually the same value as NaN. This also means
that only values of the type number, that are also NaN, return true.
Number.isNaN
和isNaN
有区别
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN
The isNaN() function determines whether a value is NaN or not.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
The Number.isNaN() method determines whether the passed value is NaN and its type is Number. It is a more robust version of the original, global isNaN().
您返回 false 的原因是 "Stack Overflow" 不是数字,而是字符串。
console.log('Number.isNaN("Stack Overflow"): '+Number.isNaN("Stack Overflow"));
console.log('isNaN("Stack Overflow"): '+isNaN("Stack Overflow"));
这 returns 正确:
console.log(isNaN("Stack Overflow"));
而这个 returns 错误:
console.log(Number.isNaN("Stack Overflow"));
我不得不这样做
Number.isNaN(Number("Stack Overflow")) // returns true
Number.isNaN(Number(false)) // returns false (unfortunately)
Number.isNaN(Number(12)) // returns false
Number.isNaN(Number(12.02)) // returns false
这看起来很奇怪,但一旦理解 NaN 在这里描述的严格意义上的“不是数字”Why does typeof NaN return 'number'?(除以零等),这就是我使用的:
typeof value === "number" && !isNaN(value)
据我理解,NaN
代表Not A Number
。 Strings 不一定是 Numbers 因此我希望下面的代码对字符串 return true
。然而,事实并非如此。
console.log(Number.isNaN("Stack Overflow"));
有人可以澄清一下吗?
不是Number.isNaN。
console.log(isNaN("Stack Overflow"));
试试看:
console.log(isNaN("Stack Overflow"));
根据MDN,非数字值首先被强制转换为数字,然后测试是否为 NaN。所以,空字符串(""
)在JS中被认为是假的,因此被转换为0,这肯定是一个数字。
In comparison to the global isNaN() function, Number.isNaN() doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to NaN, but aren't actually the same value as NaN. This also means that only values of the type number, that are also NaN, return true.
Number.isNaN
和isNaN
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN
The isNaN() function determines whether a value is NaN or not.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
The Number.isNaN() method determines whether the passed value is NaN and its type is Number. It is a more robust version of the original, global isNaN().
您返回 false 的原因是 "Stack Overflow" 不是数字,而是字符串。
console.log('Number.isNaN("Stack Overflow"): '+Number.isNaN("Stack Overflow"));
console.log('isNaN("Stack Overflow"): '+isNaN("Stack Overflow"));
这 returns 正确:
console.log(isNaN("Stack Overflow"));
而这个 returns 错误:
console.log(Number.isNaN("Stack Overflow"));
我不得不这样做
Number.isNaN(Number("Stack Overflow")) // returns true
Number.isNaN(Number(false)) // returns false (unfortunately)
Number.isNaN(Number(12)) // returns false
Number.isNaN(Number(12.02)) // returns false
这看起来很奇怪,但一旦理解 NaN 在这里描述的严格意义上的“不是数字”Why does typeof NaN return 'number'?(除以零等),这就是我使用的:
typeof value === "number" && !isNaN(value)