Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check if a number evaluates to Infinity using JavaScript?
In JavaScript, when we divide any number by zero, we get the infinity value. Also, developers can make a mistake in writing mathematical expressions which evaluate to Infinity. Before performing any operation with the returned value from mathematical expressions, we need to check if the number value is finite.
Here, we will learn three approaches to check if a number evaluates to Infinity using JavaScript.
Comparing with Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY
In JavaScript, the Number object contains POSITIVE_INFINITY and NEGATIVE_INFINITY properties that represent positive and negative infinity values. We can compare our numerical value with these properties to check if the number evaluates to Infinity.
Syntax
if (num == Number.POSITIVE_INFINITY || num == Number.NEGATIVE_INFINITY) {
// number is infinite
} else {
// number is finite
}
Example
In this example, we define two numbers with different values. The checkNumberIsFinite() function takes the number value as a parameter and prints whether the number is finite or infinite.
Comparing the number value with Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY to check if number evaluates to Infinity.
Using the Number.isFinite() Method (Recommended)
The Number.isFinite() method takes a numeric value as a parameter and returns a boolean value based on whether the number is finite. This is the most reliable approach for checking if a number evaluates to Infinity.
Syntax
if (Number.isFinite(num)) {
// number is finite
} else {
// number evaluates to infinite or NaN
}
Parameters
num ? The number to evaluate.
Return Value
It returns a Boolean value:
trueif the number is finite,falseif it's infinite or NaN.
Example
We use the Number.isFinite() method as a condition in the if-else statement. The method returns true or false based on the number value we pass as a parameter.
Using the Number.isFinite() method to check if number evaluates to Infinity.
Using Math.abs() Method and Infinity Keyword
The Math.abs() method returns the absolute value of any number. Infinity is a JavaScript keyword that represents the infinity value. We can take the absolute value of the number and compare it with Infinity.
Syntax
let number = Math.abs(num);
if (number == Infinity) {
// num is infinite
} else {
// num is finite
}
Example
The example below contains the evaluateNumber() function, which converts the number value to its absolute value and compares it with the Infinity keyword.
Using the Math.abs() method and Infinity keyword to check if number evaluates to Infinity.
Comparison of Methods
| Method | Handles NaN? | Type Coercion? | Recommendation |
|---|---|---|---|
| Direct comparison with Number.POSITIVE/NEGATIVE_INFINITY | No | Yes | Good for specific cases |
| Number.isFinite() | Yes | No | Best - most reliable |
| Math.abs() with Infinity | No | Yes | Alternative approach |
Conclusion
The best way to check if a number evaluates to Infinity is using the Number.isFinite() method, as it's the most reliable and handles edge cases like NaN. However, all three approaches are valid depending on your specific requirements.
