How to check whether a number is NaN or finite in JavaScript ?
When working with numbers in JavaScript, it's important to know how to determine if a value is NaN (Not-a-Number) or finite. This knowledge is crucial for data validation, error handling, and ensuring your code behaves as expected. In this article, we will see how to check whether the number is NaN or finite. To check whether the given number is NaN or finite, we can use JavaScript methods.
Methods to Check if a Number is NaN or Finite
JavaScript offers several built-in methods to check if a number is NaN or finite:
Table of Content
Using isNaN() Method
The isNaN() function checks whether a value is NaN. It’s a boolean function that returns true if the value is NaN and false otherwise. This function tries to convert the input into a number before checking, which can sometimes lead to unexpected results when working with non-numeric values.
Syntax:
isNan(parameter);
Example 1: This method shows the usage of the Javascript isNan() method.
function example(x) {
if (isNaN(x)) {
return 'It is NaN';
} else {
return 'It isnt NaN';
}
}
// It is not NaN
console.log(example(13));
// It is NaN
console.log(example('GeeksForGeeks'));
Output
It isnt NaN It is NaN
Note: For a more reliable check that avoids type coercion, use Number.isNaN(). This method only returns true if the value is actually NaN and not just something that can't be coerced into a number.
Using isFinite() Method
The isFinite() function checks if a number is finite. A finite number is one that is not Infinity, -Infinity, or NaN. This method converts the input to a number first, so it can return true for strings that can be converted to finite numbers.
Syntax:
isFinite(parameter);
Example 1: This method shows the use of the isFinite() method in Javascript.
function example(x) {
if (isFinite(x)) {
return 'Number is finite';
}
return 'Number is not finite';
}
console.log(example('2021/10/29'));
// Number is not finite
console.log(example(29));
// Number is finite
Output:
Number is not finite
Number is finite
Example 2: In this example, we will check for numbers if they are finite or not.
function example(x) {
if (isFinite(5 / x)) {
return 'Number is finite';
}
return 'Number is not finite';
}
console.log(example(0));
// Number is not finite
console.log(example(10));
// Number is finite
Output
Number is not finite Number is finite
Note: If needed, the isFinite() function can parse the parameter into the number
Example 3: In this example, we will check for numbers if they are finite or not using the isFinite() method of Javascript.
function example(x) {
if (isFinite(x)) {
return 'Number is finite';
}
return 'Number is not finite';
}
console.log(example('123'));
// Number is finite
console.log(example(133));
// Number is finite
console.log(example('123D'));
// Number is not finite
Output:
Number is finite
Number is finite
Number is not finite
Using Lodash isFinite() and _.isNaN() Methods
In this example, we are using Lodash's function to check whether the given number is finite or NaN.
Example: This example shows the implementation of above- explained approach.
// Defining Lodash variable
const _ = require('lodash');
let a = 10;
if (_.isFinite(a)) console.log(" it is a finite number");
if (_.isNaN(a)) console.log(" It is NaN")
Output:
it is a finite number