根据我的理解,NaN
代表 Not A Number
。 字符串 不一定是数字,因此我希望下面的代码为字符串返回true
。然而,事实并非如此。
console.log(Number.isNaN("Stack Overflow"));
有人可以澄清一下吗?
最佳答案
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"));
关于javascript - 为什么 Number.isNaN() 对字符串返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45011486/