我只在Firefox的JavaScript控制台中尝试过,但以下语句都没有返回true:

parseFloat('geoff') == NaN;

parseFloat('geoff') == Number.NaN;

当前回答

函数isNotANumber(n) { If (typeof n !== 'number') { 返回true; } == n; }

其他回答

Number('hello').toString() === 'NaN' // true
Number(undefined).toString() === 'NaN' // true
    
Number('12345').toString() === 'NaN' // false  

// These all evaluate to 0 which is a number
Number('').toString() === 'NaN' // false // 0
Number('0').toString() === 'NaN' // false // 0
Number().toString() === 'NaN' // false // 0

// These all evaluate to 0 and 1 which is a number
Number(false).toString() === 'NaN' // false // 0
Number(true).toString() === 'NaN' // false // 1

要修复'1.2geoff'被解析的问题,只需使用Number()解析器即可。

所以与其这样:

parseFloat('1.2geoff'); // => 1.2
isNaN(parseFloat('1.2geoff')); // => false
isNaN(parseFloat('.2geoff')); // => false
isNaN(parseFloat('geoff')); // => true

这样做:

Number('1.2geoff'); // => NaN
isNaN(Number('1.2geoff')); // => true
isNaN(Number('.2geoff')); // => true
isNaN(Number('geoff')); // => true

编辑:我刚刚注意到另一个问题…传入Number()的false值(true为实布尔值)返回为0!那样的话……相反,parseFloat每次都有效。所以回到这个问题:

function definitelyNaN (val) {
    return isNaN(val && val !== true ? Number(val) : parseFloat(val));
}

这似乎涵盖了一切。我测试它比lodash的_慢90%。是NaN,但它并没有涵盖所有的NaN:

http://jsperf.com/own-isnan-vs-underscore-lodash-isnan

只是为了澄清,我的关心人类字面解释的东西是“不是一个数字”和lodash的关心计算机字面解释的检查,如果什么是“NaN”。

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));

试试下面的代码:

isNaN(parseFloat("geoff"))

要检查是否有任何值是NaN,而不仅仅是数字,请参阅这里:如何在Javascript中测试NaN ?

发现这很有用

    // Long-hand const isFalsey = (value) => {   if (
        value === null ||
        value === undefined ||
        value === 0 ||
        value === false ||
        value === NaN ||
        value === ""   ) {
        return true;   }   return false; };
 // Short-hand const 
      isFalsey = (value) => !value;