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

parseFloat('geoff') == NaN;

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

当前回答

两种都试试看

if(isNaN(parseFloat('geoff'))  && typeof(parseFloat('geoff')) === "number");
//true

其他回答

找到了另一种方法,只是为了好玩。

function IsActuallyNaN(obj) {
  return [obj].includes(NaN);  
}

似乎在Node.js中,isNaN()是不被开箱即用的。 我曾经和

var value = 1;
if (parseFloat(stringValue)+"" !== "NaN") value = parseFloat(stringValue);
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

规则是:

NaN != NaN

isNaN()函数的问题是,在某些情况下,它可能会返回意想不到的结果:

isNaN('Hello')      //true
isNaN('2005/12/12') //true
isNaN(undefined)    //true
isNaN('NaN')        //true
isNaN(NaN)          //true
isNaN(0 / 0)        //true

检查该值是否真的为NaN的更好方法是:

function is_nan(value) {
    return value != value
}

is_nan(parseFloat("geoff"))