Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using an if statement to check if NaN

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?

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);
like image 926
user2084813 Avatar asked Feb 26 '13 18:02

user2084813


People also ask

How do you check if it is NaN?

1. isNaN() Method: To determine whether a number is NaN, we can use the isNaN() function. It is a boolean function that returns true if a number is NaN otherwise returns false.

How use NaN in if condition?

In JavaScript, NaN is short for "Not-a-Number". In JavaScript, NaN is a number that is not a legal number. The Number. isNaN() method returns true if the value is NaN , and the type is a Number.

How do you write if condition in NaN Python?

Use numpy. isnan(number) or math. isnan(number) instead to check if number is NaN. for i in range(len(df)): for j in range(8): if math.

Is NaN function checks whether a value is?

The isNaN() method returns true if a value is NaN. The isNaN() method converts the value to a number before testing it.

How to check if a variable is NaN or not?

The most common method to check for NaN values is to check if the variable is equal to itself. If it is not, then it must be NaN value. def isNaN (num): return num!= num x=float ("nan") isNaN (x) Output True.

How to check for Nan in Python Dataframe?

Python / April 27, 2020. Here are 4 ways to check for NaN in Pandas DataFrame: (1) Check for NaN under a single DataFrame column: df ['your column name'].isnull ().values.any () (2) Count the NaN under a single DataFrame column: df ['your column name'].isnull ().sum () (3) Check for NaN under an entire DataFrame: df.isnull ().values.any ()

How to check if Nan is true or false in JavaScript?

Use Javascript's isNaN () function. 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. Show activity on this post.

Why is Nan == NaN not always true?

Because NaN is not equal to itself, NaN != NaN will always return true. Of course, such a NaN test in your code is not always readable, so it is a good idea to use a comment or to create a wrapper function: It doesn’t make a difference if you use != or !== to check for NaN.


2 Answers

Use Javascript's isNaN() function.

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.

like image 157
Bryan Herbst Avatar answered Oct 12 '22 14:10

Bryan Herbst


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

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

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

One nice way of getting around this is:

MyNamespace.isNaN = function (x) {
  return x !== x;
}
like image 37
benekastah Avatar answered Oct 12 '22 14:10

benekastah