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

parseFloat('geoff') == NaN;

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

当前回答

要修复'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”。

其他回答

MDN的parseFloat页面中提到了另一个解决方案

它提供了一个过滤器函数来执行严格的解析

var filterFloat = function (value) {
    if(/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
      .test(value))
      return Number(value);
  return NaN;
}


console.log(filterFloat('421'));               // 421
console.log(filterFloat('-421'));              // -421
console.log(filterFloat('+421'));              // 421
console.log(filterFloat('Infinity'));          // Infinity
console.log(filterFloat('1.61803398875'));     // 1.61803398875
console.log(filterFloat('421e+0'));            // NaN
console.log(filterFloat('421hop'));            // NaN
console.log(filterFloat('hop1.61803398875'));  // NaN

然后你可以用isNaN来检查它是否是NaN

alert("1234567890.".indexOf(String.fromCharCode(mycharacter))>-1);

这并不优雅。但在尝试isNAN()后,我得到了这个解决方案,这是另一种选择。在这个例子中,我还允许'。'因为我正在为浮动进行屏蔽。您还可以反转此操作,以确保不使用任何数字。

("1234567890".indexOf(String.fromCharCode(mycharacter))==-1)

这是一个单个字符的求值,但您也可以循环遍历字符串以检查任何数字。

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

准确的检查方法是:

//takes care of boolen, undefined and empty

isNaN(x) || typeof(x) ==='boolean' || typeof(x) !=='undefined' || x!=='' ? 'is really a nan' : 'is a number'

对此我看到了一些回应,

但我只用:

function isNaN(x){
     return x == x && typeof x == 'number';
}