javascript 使用 if 语句检查是否为 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15096951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:28:30  来源:igfitidea点击:

using an if statement to check if NaN

javascriptif-statement

提问by user2084813

I'm getting a numeric value from a form. Then I check to see if it's NaN. If it is a number I want to set that value to a variable. The problem is that when I enter a valid number I still get an alert and the number isn't passed to the variable "date". How should I modify my statement so that when it is a valid number I can assign it to the variable date?

我从表单中获取数值。然后我检查它是否是 NaN。如果它是一个数字,我想将该值设置为一个变量。问题是,当我输入一个有效数字时,我仍然收到警报,并且该数字没有传递给变量“日期”。我应该如何修改我的语句,以便当它是一个有效数字时,我可以将它分配给变量日期?

var adate = document.getElementById("dueDate").value;    

    if ( adate == NaN || " ") {
    alert("Please enter a due date");
    return;
    }

    else {
    var date = (new Date()).setDate(adate);
    }

    processDate(date);

回答by Bryan Herbst

Use Javascript's isNaN()function.

使用 Javascript 的isNaN()函数。

Checking equality with NaN is always false, as per IEEE's standards. Stephen Canon, a member of the IEEE-754 committee that decided this, has an excellent answer explaining this here.

根据 IEEE 的标准,使用 NaN 检查相等性总是错误的。决定这一点的 IEEE-754 委员会成员斯蒂芬·佳能 (Stephen Canon) 有一个很好的答案,在这里解释了这一点

回答by benekastah

As strange as it seems, NaN !== NaN.

尽管看起来很奇怪,NaN !== NaN.

if (adate !== adate || adate !== " ") {
  //...
}

The isNaNfunction would work in a lot of cases. There is a good case to be made that it is broken, though.

isNaN功能在很多情况下都可以使用。不过,有一个很好的理由表明它已损坏

One nice way of getting around this is:

解决这个问题的一种好方法是:

MyNamespace.isNaN = function (x) {
  return x !== x;
}

回答by lonesomeday

You have two problems here. The result is that the conditional will always pass. This is what it does:

你在这里有两个问题。结果是条件总是会通过。这是它的作用:

adate == NaN // first, test if adate == NaN (this always returns false)
||           // if the first test fails (i.e. always), carry on checking
" "          // test if the string " " is truthy (this always returns true)

The ||does two separate checks. It does nottest to see if adateis "either NaNor " "", which seems to be what you expect.

||做两个独立的检查。它不是测试,看看是否adate是“要么NaN" "”,这似乎是你所期望的。

Your code might as well say

你的代码也可能会说

if ( true ) {

You would be able to sort this out, however, if you tried two comparisons:

但是,如果您尝试两个比较,您将能够解决这个问题:

if ( (adate == NaN) || (adate === " ")) {

As other people have said, however, this doesn't work, because NaN !== NaN. So the solution is to use isNaN:

然而,正如其他人所说,这不起作用,因为NaN !== NaN. 所以解决方案是使用isNaN

if (isNaN(adate) || (adate === " ")) {

回答by Sandun

you could Use if( isNaN(adate))

你可以使用 if( isNaN(adate))

good luck

祝你好运