我只在Firefox的JavaScript控制台中尝试过,但以下语句都没有返回true:
parseFloat('geoff') == NaN;
parseFloat('geoff') == Number.NaN;
我只在Firefox的JavaScript控制台中尝试过,但以下语句都没有返回true:
parseFloat('geoff') == NaN;
parseFloat('geoff') == Number.NaN;
当前回答
我刚刚在《有效JavaScript》这本书中看到了这个技巧,它非常简单:
因为NaN是唯一被视为与自身不相等的JavaScript值,你总是可以通过检查它是否与自身相等来测试一个值是否为NaN:
var a = NaN;
a !== a; // true
var b = "foo";
b !== b; // false
var c = undefined;
c !== c; // false
var d = {};
d !== d; // false
var e = { valueOf: "foo" };
e !== e; // false
直到@allsyed评论才意识到这一点,但这在ECMA规范中:https://tc39.github.io/ecma262/#sec-isnan-number
其他回答
你应该使用全局isNaN(value)函数调用,因为:
支持跨浏览器 有关文档,请参阅isNaN
例子:
isNaN('geoff'); // true
isNaN('3'); // false
我希望这对你有所帮助。
也许还有这个:
function isNaNCustom(value){
return value.toString() === 'NaN' &&
typeof value !== 'string' &&
typeof value === 'number'
}
marksyzm的答案工作得很好,但它不会为无穷大返回false,因为无穷大在技术上不是一个数字。
我想出了一个isNumber函数来检查它是否是一个数字。
函数isNumber(i) isNaN(i && i !== true ?编号(i): parseFloat(i)) &&[编号。POSITIVE_INFINITY, Number.NEGATIVE_INFINITY].indexOf(i) == -1; } console.log (isNumber(∞)); console.log (isNumber(“asdf ")); console.log (isNumber (1.4)); console.log (isNumber(南)); console.log (isNumber (Number.MAX_VALUE)); console.log (isNumber (" 1.68 "));
更新: 我注意到这段代码在某些参数上失败了,所以我改进了它。
function isNumber(i) {//function for checking if parameter is number if(!arguments.length) { throw new SyntaxError("not enough arguments."); } else if(arguments.length > 1) { throw new SyntaxError("too many arguments."); } else if([Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY].indexOf(i) !== -1) { throw new RangeError("number cannot be \xB1infinity."); } else if(typeof i === "object" && !(i instanceof RegExp) && !(i instanceof Number) && !(i === null)) { throw new TypeError("parameter cannot be object/array."); } else if(i instanceof RegExp) { throw new TypeError("parameter cannot be RegExp."); } else if(i == null || i === undefined) { throw new ReferenceError("parameter is null or undefined."); } else { return !isNaN(i && i !== true ? Number(i) : parseFloat(i)) && (i === i); } } console.log(isNumber(Infinity)); console.log(isNumber(this)); console.log(isNumber(/./ig)); console.log(isNumber(null));
准确的检查方法是:
//takes care of boolen, undefined and empty
isNaN(x) || typeof(x) ==='boolean' || typeof(x) !=='undefined' || x!=='' ? 'is really a nan' : 'is a number'
NaN在JavaScript中代表“不是一个数字”,尽管它的类型实际上是数字。
typeof(NaN) // "number"
要检查一个变量的值是否为NaN,我们不能简单地使用函数isNaN(),因为isNaN()有以下问题:
var myVar = "A";
isNaN(myVar) // true, although "A" is not really of value NaN
这里真正发生的是myVar被隐式地强制为一个数字:
var myVar = "A";
isNaN(Number(myVar)) // true. Number(myVar) is NaN here in fact
这是有道理的,因为A实际上不是一个数字。但我们真正想检查的是myVar的值是否为NaN。
因此isNaN()无法提供帮助。那我们该怎么办呢?
鉴于NaN是唯一被视为与自身不相等的JavaScript值,因此我们可以使用!==来检查它是否与自身相等
var myVar; // undefined
myVar !== myVar // false
var myVar = "A";
myVar !== myVar // false
var myVar = NaN
myVar !== myVar // true
所以总结一下,如果一个变量!==本身是正确的,那么这个变量的值正好是NaN:
function isOfValueNaN(v) {
return v !== v;
}
var myVar = "A";
isNaN(myVar); // true
isOfValueNaN(myVar); // false