JavaScript: How to check if a number is NaN or finite?



Checking whether a number is NaN (Not a Number) or finite is important while working with numerical computations in JavaScript. NaN (Not a Number) indicates a value that can't be represented as a number, often occurring from invalid operations like dividing zero by zero (0/0). Finite numbers are all real numbers in JavaScript that are neither Infinity, -Infinity, nor NaN.

JavaScript provides several built-in methods to determine if a value is NaN (Not a Number) or if a number is finite. In this article, you will understand how to check if a number is NaN or finite using these built-in methods.

Checking for NaN

Checking whether a number is NaN can be done in the following ways:

Using isNaN()

The isNaN() function is used to check whether a given value is NaN. This function converts the argument to a number first, then checks if it's NaN. Here is the syntax for this:

isNaN(value)

Using Number.isNaN()

The Number.isNaN method is more reliable to check whether a given value is NaN. It doesn't convert the value first and strictly checks whether a value is exactly NaN. Here is the syntax for this:

Number.isNaN(value)

Using Lodash _.isNaN()

Lodash _.isNaN() Method checks whether the given value is NaN or not. This method returns a Boolean value (Returns true if the value is NaN, else false). Here is the syntax for this:

_.isNaN(value)

Example

The following is a combined example of checking whether a value is a NaN or not using the above-mentioned methods.

// Example values to test
const values = [NaN, 'hello', 42, undefined, null, '123', 0/0];

// Using isNaN()
values.forEach(value => {
    console.log(`isNaN(${value}):`, isNaN(value));
});

// Using Number.isNaN()
values.forEach(value => {
    console.log(`Number.isNaN(${value}):`, Number.isNaN(value));
});

// Using Lodash _.isNaN()
const _ = require('lodash'); // Make sure to install lodash with npm
values.forEach(value => {
    console.log(`_.isNaN(${value}):`, _.isNaN(value));
});

Checking for Finite Numbers

Checking whether a number is finite can be done in the following ways:

Using isFinite()

The isFinite() function checks if a given value is a finite number. Same as isNaN(), it converts non-numeric values into numbers before checking. Here is the syntax for this:

isFinite(value)

Using Number.isFinite()

The Number.isFinite() method is more reliable to strictly check whether a given value is finite. It doesn't convert the value first and strictly checks whether a value is exactly finite. Here is the syntax for this:

Number.isFinite(value)

Using Lodash _.isFinite()

Lodash _.isFinite() method checks whether the given value is a primitive finite number or not and returns the corresponding boolean value. Here is the syntax for this:

_.isFinite(value);

Example

The following is a combined example of checking whether a value is a finite or not using the above-mentioned methods.

// Example values to test
const values = [Infinity, -Infinity, NaN, 42, 'hello', 0, null, '123', 100 / 2];

// Using isFinite()
values.forEach(value => {
    console.log(`isFinite(${value}):`, isFinite(value));
});

// Using Number.isFinite()
values.forEach(value => {
    console.log(`Number.isFinite(${value}):`, Number.isFinite(value));
});

// Using Lodash _.isFinite()
const _ = require('lodash'); // Make sure to install lodash with npm
values.forEach(value => {
    console.log(`_.isFinite(${value}):`, _.isFinite(value));
});

Checking if a number is NAN or Finite 

In the example below, we are going to use the above method to check if the number is finite or NaN. The above method will either return true or false, and based upon that, we can decide if the number is finite or NaN.

function isFinite(x) {
   if(isNaN(x)) {
      return 'It is NaN';
   } else {
      return 'It is a Finite Number';
   }
}
console.log(isFinite(21));
console.log(isFinite('Tutorials Point'));

Conclusion

In this article, we have seen three different methods for checking whether a number is NaN or finite. All methods are reliable and versatile. Understanding the differences between these methods and their variants helps in writing robust numerical computations in JavaScript.

Updated on: 2025-03-07T13:01:54+05:30

528 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements