JavaScript isNaN 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8923119/
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
JavaScript isNaN function
提问by Lina
I am using isNan functionto check if entered value is a number:
我正在使用 isNan 函数来检查输入的值是否为数字:
if(isNaN(num)) {
alert('I am not a number');
}
else {
//
}
However, if entered value is, for example, "5748hh" it is understandable like a number and return false
. What is wrong?
但是,如果输入的值是,例如,“5748hh”,它就像一个数字和 return 是可以理解的false
。怎么了?
回答by Fabrizio Calderan
from Mozilla MDN
来自 Mozilla MDN
When the argument to the isNaN function is not a number, the value is first coerced to a number. The resulting value is then tested to determine whether it is NaN. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one), the "false" returned value may be unexpected; the empty string, for example, is surely "not a number." The confusion stems from the fact that the term, "not a number", has a specific meaning for numbers represented as IEEE-794 floating-point values. The function should be interpreted as answering the question, "is this value, when coerced to a numeric value, an IEEE-794 'Not A Number' value?"
当 isNaN 函数的参数不是数字时,该值首先被强制转换为数字。然后测试结果值以确定它是否为 NaN。因此,对于非数字,当被强制转换为数字类型时会产生一个有效的非 NaN 数值(特别是空字符串和布尔基元,当被强制时给出数值零或一),“假”返回值可能是意外的;例如,空字符串肯定是“不是数字”。混淆源于这样一个事实,即术语“不是数字”对于表示为 IEEE-794 浮点值的数字具有特定含义。该函数应该被解释为回答这个问题,“当这个值被强制转换为一个数值时,是一个 IEEE-794 '非数字'值吗?”
so alert(isNaN('45345ll'))
returns true because its value coercion is different than a parseInt/parseFloat
conversion.
soalert(isNaN('45345ll'))
返回 true,因为它的值强制parseInt/parseFloat
转换与转换不同。
e.g.
例如
var num = false;
console.log(isNaN(num)); /* return false because the coerced value is 0 but */
console.log(parseInt(num, 10)); /* return isNaN */
Maybe you get an unexpected value due to a missing paren in your if
statement
由于if
语句中缺少括号,您可能会得到一个意外的值
回答by Matt Fellows
if you parse a string that starts with a number it will successfully parse into the number, with the remainder of the string ignored. e.g. parseInt('56sadfh', 10)
will parse into 56
. If you want to prevent this you should use a regex to check the input instead. e.g.
如果您解析以数字开头的字符串,它将成功解析为该数字,而忽略字符串的其余部分。例如parseInt('56sadfh', 10)
将解析为56
. 如果你想防止这种情况,你应该使用正则表达式来检查输入。例如
if (!num.match(/[0-9]+/)) {
alert("not a number");
}
回答by Shadow Wizard is Ear For You
You must try to parse it first:
您必须先尝试解析它:
if(isNaN(parseInt(num, 10))) {
According to the documentation:
根据文档:
Since the very earliest versions of the isNaN function specification, its behavior for non-numeric arguments has been confusing. When the argument to the isNaN function is not a number, the value is first coerced to a number. The resulting value is then tested to determine whether it is NaN. Thus for non-numbers that when coerced to numeric type result in a valid non-NaN numeric value (notably the empty string and boolean primitives, which when coerced give numeric values zero or one)
由于 isNaN 函数规范的最早版本,它对非数字参数的行为一直令人困惑。当 isNaN 函数的参数不是数字时,该值首先被强制转换为数字。然后测试结果值以确定它是否为 NaN。因此,对于非数字,当强制为数字类型时会产生有效的非 NaN 数值(特别是空字符串和布尔基元,当被强制时给出数值零或一)
Unlike parseInt()
this coerced to a numberwill result in Not a Number for string like 5748hh
and hence the behavior you see.
与parseInt()
此不同,强制为数字将导致 Not a Number for string like5748hh
以及您看到的行为。
回答by Eirinn
isNaN = is Not a Number, so if it returns true the data inputted is not a number.
isNaN = 不是数字,所以如果它返回 true 输入的数据不是数字。
if(isNaN("5748hh"))
{
alert("not a number");
}
else
{
alert("a number");
}
Returns true: "not a number", like it should.
返回 true:“不是数字”,就像它应该的那样。
回答by kisp
NaN is a special number. isNaN(argument) function checks the argument if it is a NaN value, but it also gives true if the argument is a string, representing a valid integer.
NaN 是一个特殊的数字。isNaN(argument) 函数检查参数是否为 NaN 值,但如果参数是字符串,则它也会返回 true,表示有效整数。
回答by xdazz
isNaN("5748hh");
returns true
.
isNaN("5748hh");
返回true
。
回答by gdoron is supporting Monica
回答by Jason
In my Firebug:
在我的萤火虫中:
>>> isNaN("5748hh")
true