为什么 Number.isNaN() 为字符串返回 false? [英] Why does Number.isNaN() return false for Strings?

查看:62
本文介绍了为什么 Number.isNaN() 为字符串返回 false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解 NaN 代表 Not A Number.字符串 不一定是数字,因此我希望下面的代码为字符串返回true.然而,事实并非如此.

As per my understanding NaN stands for Not A Number. Strings are not definitely Numbers and hence I expect the below code to return true for Strings. However, it's not the case.

console.log(Number.isNaN("Stack Overflow"));

有人可以澄清一下吗?

推荐答案

Number.isNaNisNaN

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/isNaN

isNaN() 函数确定一个值是否为 NaN.

The isNaN() function determines whether a value is NaN or not.

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN

Number.isNaN() 方法判断传入的值是否为 NaN,类型是否为 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 的原因是堆栈溢出"不是数字,而是字符串.

The reason you are returning false is that "Stack Overflow" is not a number it is a string.

console.log('Number.isNaN("Stack Overflow"): '+Number.isNaN("Stack Overflow"));
console.log('isNaN("Stack Overflow"): '+isNaN("Stack Overflow"));

这篇关于为什么 Number.isNaN() 为字符串返回 false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆