How to check if a number is infinity using JavaScript?

王林
Release: 2023-09-09 14:45:03
forward
1087 people have browsed it

如何使用 JavaScript 检查数字是否为无穷大?

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.

Compare numeric values to Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY

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.

grammar

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 }
Copy after login

In the above syntax, we have used theOR (||)operator to evaluate multiple conditions in the if statement.

Example

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.

Copy after login

Use isFinite() method

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.

grammar

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 }
Copy after login

parameter

  • num1- This is a number to be evaluated.

return value

  • It returns a boolean value depending on whether the number is finite or infinite.

Example

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.

Copy after login

Use Math.abs() method and Infinity keyword

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.

grammar

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 }
Copy after login

Example

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.


Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!