How to check whether a number is NaN or finite in JavaScript ?
Last Updated :
16 Sep, 2024
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:
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.
JavaScript
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'));
OutputIt 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.
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.
JavaScript
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
OutputNumber 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.
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.
JavaScript
// 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
Similar Reads
Check a Number is Prime or Not Using JavaScript A prime number is a whole number greater than 1, which has no positive divisors other than 1 and itself. In other words, prime numbers cannot be formed by multiplying two smaller natural numbers. For example:2, 3, 5, 7, 11, and 13 are prime numbers.4, 6, 8, 9, and 12 are not prime numbers because th
5 min read
JavaScript - Convert a Number into JS Array You have a number, like 12345, and you need to convert it into an array where each element represents a digit of the number. For example, 12345 should become [1, 2, 3, 4, 5]. How can you achieve this in JavaScript?In JavaScript, there are various ways to transform a number into an array of its digit
3 min read
How to generate a n-digit number using JavaScript? The task is to generate an n-Digit random number with the help of JavaScript. You can also generate random numbers in the given range using JavaScript. Below are the approaches to generate a n-digit number using JavaScript: Table of Content Using Math.random()Math.random() Method and .substring() Me
2 min read
How to get decimal portion of a number using JavaScript ? Given a float number, The task is to separate the number into integer and decimal parts using JavaScript. For example, a value of 15.6 would be split into two numbers, i.e. 15 and 0.6 Here are a few methods discussed. These are the following methods: Table of Content Javascript String split() Method
3 min read
Generate Random Number in Given Range Using JavaScript Here are the different ways to generate random numbers in a given range using JavaScript1. Using Math.random(): basic ApproachThis is the simplest way to generate a random number within a range using Math.random().JavaScriptlet min = 10; let max = 20; let random = Math.floor(Math.random() * (max - m
3 min read
How to convert long number into abbreviated string in JavaScript ? We are given a long number and the task is to convert it to the abbreviated string(eg.. 1234 to 1.2k). Here 2 approaches are discussed with the help of JavaScript.Approaches to Convert Long Number to Abbreviated String:Table of ContentUsing JavaScript methodsUsing Custom functionUsing logarithmsUsin
6 min read
How to check first number is divisible by second one in JavaScript ? Given two numbers and the task is to check the first number is divisible by the second number or not with the help of JavaScript. Before getting into the coding part, first let us know about the modulo operator and triple equals. To find a number is divisible by another or not, we simply use the rem
2 min read
JavaScript - How to Get a Number of Vowels in a String? Here are the various methods to get the number of vowels in a string using JavaScript.1. Using a for LoopThis is the most basic and beginner-friendly approach. It uses a loop to iterate over each character and checks if it is a vowel.JavaScriptconst cVowels = (s) => { const vowels = "aeiouAEIOU";
3 min read
How to limit a number between a min/max value in JavaScript ? We can limit a number between a min/max value using the is-else condition and using the Math.min and Math.max methods of JavaScript.Below are the approaches to limit a number between a min/max value in JavaScript:Table of ContentUsing the if-else conditionUsing the Math.min() and Math.max() methodsU
2 min read
How to convert a pixel value to a number value using JavaScript ? In this article, we will see how to convert the string value containing 'px' to the Integer Value with the help of JavaScript. There are two approaches to converting a pixel value to a number value, these are: Using parseInt() MethodUsing RegExpApproach 1: Using parseInt() Method This method takes a
2 min read