Javascript Learn Infinity Property Basics|number isFinite method example

What is infinity in JavaScript?

Infinity is a global object property. It is used to represent the maximum value of a number. It contains values of POSITIVE_INFINITY and NEGATIVE_INFINITY. number in javascript is always less than POSITIVE_INFINITY

It is used to represent the result of mathematical calculations with overflow or underflow values.

Infinity Property in javascript

Infinity is a property of a javascript Global Object, that specifies positive values of numeric data. The default value of infinity is Number.POSITIVE_INFINITY:

It represents the Mathematical Infinity value. In mathematical calculations, Any value divided by Zero is returned by infinity.



Javascript Infinity Example

The number value is divided by Zero. and the Infinity value is always the largest value of any numeric value When you are adding/multiply any value with infinity will get an Infinity

console.log(123 / 0); // Infinity
console.log(Infinity); // Infinity
console.log(Infinity - Infinity); // NaN
console.log(Infinity + Infinity); // Infinity
console.log(Infinity * Infinity); // Infinity

Infinity value is represented in Positive and Negative Number.POSITIVE_INFINITY - Represents Positive infinite values - Infinity Number.NEGATIVE_INFINITY - Represents Negative Infinite value - -Infinity Infinity property usages

  1. This will be used to check arithmetic operations result in comparison with Infinity.
  2. This will also be used to store the infinite value of any calculations
  3. Checking infinite value for Division

How to compare Infinity value in javascript

Infinity can be used as a variable in conditional if the expression

var value = 45;
if (45< Infinity>)
console.log('Value is less than the infinite value')

output is

Value is less than the infinite value

javascript Number isFinite() methods

isFinite() is defined in the Number Object. isFinite() method checks the value for a finite number, returns true for a finite number, and false for an infinite number.



The example Following is To check Given number is finite or infinite using the isFinite() method.

console.log(Number.isFinite(123)); // true
console.log(Number.isFinite(Infinity)); //false
console.log(Number.isFinite(null)); //false
console.log(Number.isFinite(NaN)); //false
console.log(Number.isFinite(-Infinity)); //false
console.log(Number.isFinite(undefined)); //false

How do you write positive infinity in JavaScript?

To write POSITIVE_INFINITY, please follow the below steps

  • it is used as a number.POSITIVE_INFINITY
  • you can use the compare operator to check numbervariable===number.POSITIVE_INFINITY in conditional statements.
  • Mathematical calculations with this always returns either INFINITY or NAN.