我只在Firefox的JavaScript控制台中尝试过,但以下语句都没有返回true:
parseFloat('geoff') == NaN;
parseFloat('geoff') == Number.NaN;
我只在Firefox的JavaScript控制台中尝试过,但以下语句都没有返回true:
parseFloat('geoff') == NaN;
parseFloat('geoff') == Number.NaN;
虽然 @chiborg 的回答是正确的,但还有一些需要注意的事项:
parseFloat('1.2geoff'); // => 1.2
isNaN(parseFloat('1.2geoff')); // => false
isNaN(parseFloat('.2geoff')); // => false
isNaN(parseFloat('geoff')); // => true
如果你正在使用此方法进行输入验证,那么结果会相当自由。
所以,是的你可以使用parseFloat(string)
(或在完整数字的情况下使用parseInt(string, radix)
),然后随后将其包装在isNaN()
中,但请注意数字与其他非数字字符交织的陷阱。
parseFloat('test1.2')
将返回NaN
。 - Eduard Luca真的超级简单!在这里!使用这个方法!
function isReallyNaN(a) { return a !== a; };
使用起来就像这样简单:
if (!isReallyNaN(value)) { return doingStuff; }
此外:请参见第一个示例以获得一些备选实现。
function isReallyNaN(a) { return a !== a; };
var example = {
'NaN': NaN,
'an empty Objet': {},
'a parse to NaN': parseFloat('$5.32'),
'a non-empty Objet': { a: 1, b: 2 },
'an empty Array': [],
'a semi-passed parse': parseInt('5a5'),
'a non-empty Array': [ 'a', 'b', 'c' ],
'Math to NaN': Math.log(-1),
'an undefined object': undefined
}
for (x in example) {
var answer = isReallyNaN(example[x]),
strAnswer = answer.toString();
$("table").append($("<tr />", { "class": strAnswer }).append($("<th />", {
html: x
}), $("<td />", {
html: strAnswer
})))
};
table { border-collapse: collapse; }
th, td { border: 1px solid; padding: 2px 5px; }
.true { color: red; }
.false { color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table></table>
如果您不想使用另外命名的方法,并希望确保全局可用性,那么您可以采取一些替代实现路径。
警告这些解决方案涉及更改本地对象,可能不是最佳解决方案。请始终谨慎使用,并注意您可能使用的其他库可能依赖于本地代码或类似更改。
isNaN
方法。// Extremely simple. Just simply write the method.
window.isNaN = function(a) { return a !==a; }
Number['isNaN'] || (Number.isNaN = function(a) { return a !== a });
// Use as simple as
Number.isNaN(NaN)
备选解决方案 测试是否为空
我写的一个简单的窗口方法,可以测试对象是否为空。 不同之处在于它不会返回是否是“完全” NaN ,但我认为我会发布这个作为备选方案,因为当查找空项目时也可能非常有用。
/** isEmpty(varried)
* Simple method for testing if item is "empty"
**/
;(function() {
function isEmpty(a) { return (!a || 0 >= a) || ("object" == typeof a && /\{\}|\[(null(,)*)*\]/.test(JSON.stringify(a))); };
window.hasOwnProperty("empty")||(window.empty=isEmpty);
})();
;(function() {
function isEmpty(a) { return !a || void 0 === a || a !== a || 0 >= a || "object" == typeof a && /\{\}|\[(null(,)*)*\]/.test(JSON.stringify(a)); };
window.hasOwnProperty("empty")||(window.empty=isEmpty);
})();
var example = {
'NaN': NaN,
'an empty Objet': {},
'a parse to NaN': parseFloat('$5.32'),
'a non-empty Objet': { a: 1, b: 2 },
'an empty Array': new Array(),
'an empty Array w/ 9 len': new Array(9),
'a semi-passed parse': parseInt('5a5'),
'a non-empty Array': [ 'a', 'b', 'c' ],
'Math to NaN': Math.log(-1),
'an undefined object': undefined
}
for (x in example) {
var answer = empty(example[x]),
strAnswer = answer.toString();
$("#t1").append(
$("<tr />", { "class": strAnswer }).append(
$("<th />", { html: x }),
$("<td />", { html: strAnswer.toUpperCase() })
)
)
};
function isReallyNaN(a) { return a !== a; };
for(x in example){var answer=isReallyNaN(example[x]),strAnswer=answer.toString();$("#t2").append($("<tr />",{"class":strAnswer}).append($("<th />",{html:x}),$("<td />",{html:strAnswer.toUpperCase()})))};
table { border-collapse: collapse; float: left; }
th, td { border: 1px solid; padding: 2px 5px; }
.true { color: red; }
.false { color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table id="t1"><thead><tr><th colspan="2">isEmpty()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table>
<table id="t2"><thead><tr><th colspan="2">isReallyNaN()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table>
这个方法会比较深入地检查一个对象是否为空,甚至会检查一个对象是否只包含空对象。我相信它还有改进的空间和可能存在的问题,但目前看来,它似乎可以捕捉到大部分情况。
function isEmpty(a) {
if (!a || 0 >= a) return !0;
if ("object" == typeof a) {
var b = JSON.stringify(a).replace(/"[^"]*":(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '').replace(/"[^"]*":\{\},?/g, '');
if ( /^$|\{\}|\[\]/.test(b) ) return !0;
else if (a instanceof Array) {
b = b.replace(/(0|"0*"|false|null|\{\}|\[(null(,)?)*\]),?/g, '');
if ( /^$|\{\}|\[\]/.test(b) ) return !0;
}
}
return false;
}
window.hasOwnProperty("empty")||(window.empty=isEmpty);
var example = {
'NaN': NaN,
'an empty Objet': {},
'a parse to NaN': parseFloat('$5.32'),
'a non-empty Objet': { a: 1, b: 2 },
'an empty Array': new Array(),
'an empty Array w/ 9 len': new Array(9),
'a semi-passed parse': parseInt('5a5'),
'a non-empty Array': [ 'a', 'b', 'c' ],
'Math to NaN': Math.log(-1),
'an undefined object': undefined,
'Object Full of Empty Items': { 1: '', 2: [], 3: {}, 4: false, 5:new Array(3), 6: NaN, 7: null, 8: void 0, 9: 0, 10: '0', 11: { 6: NaN, 7: null, 8: void 0 } },
'Array Full of Empty Items': ["",[],{},false,[null,null,null],null,null,null,0,"0",{"6":null,"7":null}]
}
for (x in example) {
var answer = empty(example[x]),
strAnswer = answer.toString();
$("#t1").append(
$("<tr />", { "class": strAnswer }).append(
$("<th />", { html: x }),
$("<td />", { html: strAnswer.toUpperCase() })
)
)
};
function isReallyNaN(a) { return a !== a; };
for(x in example){var answer=isReallyNaN(example[x]),strAnswer=answer.toString();$("#t2").append($("<tr />",{"class":strAnswer}).append($("<th />",{html:x}),$("<td />",{html:strAnswer.toUpperCase()})))};
table { border-collapse: collapse; float: left; }
th, td { border: 1px solid; padding: 2px 5px; }
.true { color: red; }
.false { color: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table id="t1"><thead><tr><th colspan="2">isEmpty()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table>
<table id="t2"><thead><tr><th colspan="2">isReallyNaN()</th></tr></thead><thead><tr><th>Value Type</th><th>Bool Return</th></tr></thead></table>
isNaN
不可靠。有人在那里评论并告诉我这个问题更适合我的答案,因此将其移动到了这里。只是简单地提供一些与此问题相关的有用信息,这个问题在Google搜索中会出现,“它是NaN吗?”值得一提的是,我的答案比选定答案更准确。 - SpYk3HHfunction isNotANumber(n) {
if (typeof n !== 'number') {
return true;
}
return n !== n;
}
Number.isNaN
确保值确实为NaN
。
isNaN
的问题是,如果您将其用于非数字数据,则会应用一些令人困惑的规则(根据MDN)。例如,isNaN(NaN); // true
isNaN(undefined); // true
isNaN({}); // true
Number.isNaN(parseFloat('geoff'))
isNaN
可能会让你陷入麻烦,并记得在ECMAScript 2015环境中使用Number.isNaN
:-) - thefourtheye我想分享另一种选择,它不一定比其他选择更好,但我认为值得一看:
function customIsNaN(x) { return (typeof x == 'number' && x != 0 && !x); }
0
和 NaN
以外的每个数字都会被转换为 true
。Number.isNaN
和检查自身是否为假一样好。所有三者的表现都比 isNan
好。customIsNaN(NaN); // true
customIsNaN(0/0); // true
customIsNaN(+new Date('?')); // true
customIsNaN(0); // false
customIsNaN(false); // false
customIsNaN(null); // false
customIsNaN(undefined); // false
customIsNaN({}); // false
customIsNaN(''); // false
如果您想避免破损的isNaN
函数,这可能会很有用。
undefined
时,为什么isNaN会返回true是错误的行为呢?不是undefined
确实不是一个数字吗?请您帮我澄清一下。 - Xaxisundefined
可能与 NaN
不同,但它仍然不是一个数字,因此 isNaN(undefined)
应该是(并且是)true
。 - neuhausfunction isNaNCustom(value){
return value.toString() === 'NaN' &&
typeof value !== 'string' &&
typeof value === 'number'
}
似乎Node.js默认不支持isNaN()函数。
我通过以下方法进行了解决:
var value = 1;
if (parseFloat(stringValue)+"" !== "NaN") value = parseFloat(stringValue);
NaN === NaN; // false
Number.NaN === NaN; // false
isNaN(NaN); // true
isNaN(Number.NaN); // true
等值运算符(==和===)不能用来测试值是否为NaN。
参考 Mozilla 文档中的描述:“全局 NaN 属性是一个表示非数字的值。”
最好的方法是使用内置函数'isNaN()'来检查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