nodejs 中的 isNaN 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24078017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
isNaN function in nodejs
提问by azero0
why is isNaN function in nodejs returning falsein the following cases?
为什么在以下情况下nodejs 中的 isNaN 函数返回false?
isNaN(''),isNaN('\n'),isNaN('\t')
this is very weird.
这很奇怪。
does somebody have any ideas as I thought isNaN stood for is Not a Number.
有人有任何想法,因为我认为 isNaN 代表的不是数字。
can someone please clarify
有人可以澄清一下吗
Thanks in advance!
提前致谢!
回答by Esailija
Because you are not passing it a number, it will convert it to number. All of those convert to 0which is 0and not NaN
因为你没有传递给它一个数字,它会将它转换为数字。所有这些都转换为0是0和不是NaN
Number('')
0
Number('\n')
0
Number('\t')
0
isNaN(0)
false
Note that NaNdoes not stand for "not a JavaScript Number". In fact it's completely separate from JavaScriptand exists in all languages that support IEEE-754 floats.
请注意,这NaN不代表“不是 JavaScript 编号”。事实上,它完全独立于 JavaScript,存在于所有支持 IEEE-754 浮点数的语言中。
If you want to check if something is a javascript number, the check is
如果你想检查某个东西是否是一个 javascript 数字,检查是
if (typeof value === "number") {
}
回答by Phil H
NaN is a very specific thing: it is a floating point value which has the appropriate NaN flags set, per the IEEE754 spec (Wikipedia article).
NaN 是一个非常具体的东西:它是一个浮点值,根据 IEEE754 规范(维基百科文章)设置了适当的 NaN 标志。
If you want to check whether a string has a numeric value in it, you can do parseFloat(str)(MDN on parseFloat). If that fails to find any valid numeric content, or finds invalid characters before finding numbers, it will return a NaN value.
如果你想检查一个字符串中是否有一个数值,你可以这样做parseFloat(str)(parseFloat 上的 MDN)。如果未能找到任何有效的数字内容,或者在找到数字之前找到无效字符,它将返回一个 NaN 值。
So try doing isNaN(parseFloat(str))- it gives me truefor all three examples posted.
所以尝试这样做isNaN(parseFloat(str))- 它为我true提供了所有三个发布的示例。
回答by Uberbrady
isNan()is designed to help detect things that are 'mathematically undefined' - e.g. 0/0-
isNan()旨在帮助检测“数学上未定义”的事物 - 例如0/0-
node
> isNaN(0/0)
true
> isNaN(1/0)
false
> isNaN(Math.sqrt(-1))
true
> isNaN(Math.log(-1))
true
The other advice you got here in this question on how to detect numbers is solid.
你在这个问题中得到的关于如何检测数字的另一个建议是可靠的。
回答by manespgav
isNaN is a "is Not a Number" function, it returns true when no number are give to it as parameter, and false when a number is given
isNaN 是一个“不是数字”的函数,当没有给它数字作为参数时它返回真,当给它一个数字时返回假

