How To Check A Number Is NaN In JavaScript

In this article, you will learn how to Check A Number Is NaN in JavaScript with Code Examples.

When working with javascript, we may be asked to determine whether a value is NaN. This article demonstrates how to quickly determine whether a variable is NaN. In javascript, NaN is a global property that has the value Not-a-Number.

There are various inbuilt functions available in JavaScript that you can use to check if the variable is NaN or not. One such function is isNaN(). Let us discuss in the detail each of these methods that you can use to check variables for NaN in JavaScript.

1. Using isNaN() Function To Check A Number is NaN In JavaScript

The function isNaN()[1] provides you output as True when the input is not a number. And it returns the output as False when the input is either Number or a number as a string.

Let us check in the below example code the usage of the isNaN function in JavaScript to check if a variable is NaN.

//Creating Function to Check if Number is NaN
function isNumberNaN(inputVariable){
    //This Function will return True if 
    //Variable is Nan Else It will Return
    //False
    return isNaN(inputVariable);
}

let listOfValues = [1, 2, 'CheckThis', '442'];

for(var index in listOfValues){
    let isVarNaN = isNumberNaN(listOfValues[index])
    if(isVarNaN){
        console.log("This variable is NaN.")
    }else{
        console.log("This variable is not NaN.")
    }
}

I was able to print whether the elements in the list are NaN or not, as you can see in the code above. Only one element is present in the above list: the only string that does not contain a single number; as a result, the above code refers to that element as NaN.

2. Using Number.isNaN Function For Verifying If Number is NaN

For consistency issues in isNaN() function, JavaScript does not recommend using that function instead ask the developers to use the Number.isNaN() function.

The Number.isNaN() method checks if the passed value is NaN and of the Number type. It’s a more robust version of the global isNaN() function. Let us see in the below example code the usage of the Number.isNaN() function in JavaScript.

//Creating Function to Check if Number is NaN
function isNumberNaN(inputVariable){
    //This Function will return True if 
    //Variable is Nan Else It will Return
    //False
    return Number.isNaN(Number(inputVariable));
}

let listOfValues = [1, 2, 'CheckThis', '442'];

for(var index in listOfValues){
    let isVarNaN = isNumberNaN(listOfValues[index])
    if(isVarNaN){
        console.log("This variable is NaN.")
    }else{
        console.log("This variable is not NaN.")
    }
}

Output:

This variable is not NaN.
This variable is not NaN.
This variable is NaN.
This variable is not NaN.

In the above code, I have first typecasted the elements of the list into Number and then using the Number.isNaN() verified if the element is NaN or not. As only one element from the list is NaN and hence in the above output we get one variable as NaN.

3. Using Not Equal to Operator (Variable !== NaN)

To check if a Number is NaN or not you can also use the not equal to operator. But before you check if the input variable is NaN you will require it to be typecasted into Number.

Let us check in the below example code how to use the Not Equal Operator To Check NaN in JavaScript.

//Creating Function to Check if Number is NaN
function isNumberNaN(inputVariable){
    //This Function will return True if 
    //Variable is Nan Else It will Return
    //False
    if(Number(inputVariable) !== Number(inputVariable)){
        return true;
    }else{
        return false;
    }
}

let listOfValues = [1, 2, 'CheckThis', '442'];

for(var index in listOfValues){
    let isVarNaN = isNumberNaN(listOfValues[index])
    if(isVarNaN){
        console.log("This variable is NaN.")
    }else{
        console.log("This variable is not NaN.")
    }
}

Output:

This variable is not NaN.
This variable is NaN.
This variable is not NaN.

As you can see in the code above, non-numbers are typecast to NaN, so you can’t compare two NaNs. This means that if the condition is true, it returns true.

4. Using toString() To Check If Number Variable is NaN

If you have a variable that holds a Number and then uses the toString function on that number, you’ll get a NaN. However, you must first typecast the variable to Number.

Let us check in the below code the usage of the toString() function to check if the variable is NaN or Not.

//Creating Function to Check if Number is NaN
function isNumberNaN(inputVariable){
    //This Function will return True if 
    //Variable is Nan Else It will Return
    //False
    return Number(inputVariable).toString() !== 'NaN';
}

let listOfValues = [1, 2, 'CheckThis', '442'];

for(var index in listOfValues){
    let isVarNaN = isNumberNaN(listOfValues[index])
    if(!isVarNaN){
        console.log("This " + listOfValues[index] + " variable is NaN.")
    }else{
        console.log("This " + listOfValues[index] + " variable is not NaN.")
    }
}

Output:

This 1 variable is not NaN.
This 2 variable is not NaN.
This CheckThis variable is NaN.
This 442 variable is not NaN.

Only the string ‘CheckThis’ is set as NaN when I typecast it to a number and used the toString() function on it, as you can see in the above code. All other elements in the list, like the first, represent a number and are thus not equal to NaN.

5. Using include function to Check if Number Variable is NaN

The include function in Javascript allows you to check if a variable contains a NaN. If the variable contains NaN, this function returns true; otherwise, it returns False.

Let us see in the below example code the usage of include function to check NaN in JavaScript.

//Creating Function to Check if Number is NaN
function isNumberNaN(inputVariable){
    //This Function will return True if 
    //Variable is Nan Else It will Return
    //False
    return [Number(inputVariable)].includes(NaN);
}

let listOfValues = [1, 2, 'CheckThis', '442'];

for(var index in listOfValues){
    let isVarNaN = isNumberNaN(listOfValues[index])
    if(isVarNaN){
        console.log("This " + listOfValues[index] + " variable is NaN.")
    }else{
        console.log("This " + listOfValues[index] + " variable is not NaN.")
    }
}

Output:

This 1 variable is not NaN.
This 2 variable is not NaN.
This CheckThis variable is NaN.
This 442 variable is not NaN.

As you can see includes method works with an object and hence after typecasting the number variable you will need to use it in the bracket to make it can list the object and then you can use the includes function to check if the list object contains NaN.

How To Check A Number Is NaN In JavaScript

Wrap Up

I hope you got the answer related to the question on how to check if the number is NaN in JavaScript. I have listed down the five methods that you can use.

The second method on the list above is the one you should use in your code. Because this is standard programming practice and considered best practice in production code, it should be used.

Please let me know in the comment section if you know any better method than the one discussed above I will be happy to share it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Code Example: PopCat Click Hack Code
  2. Best Way To Validate Email Address In JavaScript Regex
  3. How To Make The First Letter Of A String Uppercase in JavaScript
  4. How To Test For An Empty Object In JavaScript
  5. Code Examples: How To Format A Date in JavaScript
  6. How To Remove First Character From A String in JavaScript
  7. How To Fix Cannot use import statement outside a module JavaScript
  8. How To Remove A Specific item From An Array
  9. Best Udemy JavaScript Course 2021
How To Check A Number Is NaN In JavaScript

Leave a Comment