In JavaScript, when we divide any number by zero, we can get infinity value. Additionally, developers may make mistakes when writing mathematical expressions that evaluate to infinity. Therefore, before performing any operations on the return value of a mathematical expression, we need to check whether the value is finite.
Here we will learn three ways to check if a number is infinity using JavaScript.
In JavaScript, a number is an object that contains different properties and methods related to numbers. ThePOSITIVE_INFINITYandNEGATIVE_INFINITYproperties of the Number object allow developers to evaluate the positive and negative infinity values of a number.
We can compare the numeric value withNumber.POSITIVE_INFINITYandNumber.NEGATIVE_INFINITYto check if number evaluates to Infinity.
Use thePOSITIVE_INFINITYandNEGATIVE_INFINITYproperties of numeric objects according to the following syntax.
if (num1 == Number.POSITIVE_INFINITY || num1 == Number.NEGATIVE_INFINITY) { // number is finite } else { // number is not finite }
In the above syntax, we have used theOR (||)operator to evaluate multiple conditions in the if statement.
In this example, we define two numbers with different values. num1 contains finite values and num2 contains infinite values. The checkNumberIsFinite() function takes a numeric value as an argument and prints the message accordingly by comparing the number withPOSITIVE_INFINITYandNEGATIVE_INFINITY, regardless of whether the number is finite or not.
Comparing the number value with the Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY to check if number evaluates to Infinity.
isFinite()The method takes a numeric value as a parameter and returns a Boolean value depending on whether the number is finite. Here we will call theisFinite()method with the Number object as a reference to evaluate the number more robustly.
Users can use theisFinite()method according to the following syntax to check whether the number is infinite. We take the Number object as a reference and pass the numeric value as a parameter.
if (Number.isFinite(num1)) { // number is finite } else { // number evaluates to infinite }
num1- This is a number to be evaluated.
It returns a boolean value depending on whether the number is finite or infinite.
We use the isFinite() method as the condition of the if-else statement. The isFinite() method returns true or false based on the value we pass as parameter. Depending on the return value, control of program execution goes to the if or else block.
Using the isFinite() method to check if number evaluates to Infinity.
Math.abs()method allows us to get the absolute value of any number.Infinityis a keyword in JavaScript that represents an infinite value.
We can compare our number to bothinfinityand -infinity, or take the absolute value of the number and compare it only to infinity.
Users can use the following syntax to use theMath.abs()method and theInfinitykeyword to check whether the calculation result of number isInfinity.
let number = Math.abs(num); if (number == Infinity) { // num is not finite. } else { // num is finite }
The example below contains theevaluateNumber()function, which is called when the user clicks the Evaluate Number button.evaulateNumber()The function first converts the numeric value to a positive value and compares it with the Infinity keyword.
Using the Math.abs() method and Infinity keyword to check if number evaluates to Infinity.
The best way to check if a number is infinity is to use the isFinite() method, which takes a number as a parameter and returns the result after calculating the number. However, users can also use other methods since all methods are linear.
The above is the detailed content of How to check if a number is infinity using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!