What is Infinity in JavaScript?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

Infinity is a property of the global object, which means it is a globally scoped variable. It is a number that exceeds any other number.

The value of this variable is Positive Infinity, which means that it is bigger than any other positive number.

The Definition of Infinity

The Concept of JavaScript Infinity comes from mathematics. In mathematics, infinity represents the concept of something that is without bound, that is endless, that is unlimited. It is represented by

Let's deep dive into JavaScript Infinity.

In JavaScript, there are two special values

  • Positive Infinity represented by +Infinity
  • Negative Infinity represented by -Infinity

These are special types of numbers

Output

In simple words, Infinity represents a number bigger than any positive number and -Infinity represents a number smaller than any negative number.

You might be wondering why we need all these!!! The thing is, these prove to be very helpful while creating different types of logic. We will cover some scenarios later in this article.

The Properties of Infinity

JavaScript infinity follows the same properties as Infinity in mathematics. Let's learn more about the properties of JavaScript Infinity.

Infinity is Bigger than any Finite Number

Let's use the code to prove this property

Output

In the code, we tried to check if Infinity is greater than MAX_SAFE_INTEGER and the result is true.

The MAX_SAFE_INTEGER represents the maximum safe integer that can be used in JavaScript (2532^{53}11).

Using Infinity as Operand in Math Operations

  • Addition Adding value to Infinity results in Infinity

    Output

    What if we add Infinity to Infinity?

    Output

  • Multiplication Similar to addition, multiplying anything by Infinity results in an Infinity

    Output

  • Division The division is NOT the same as addition and multiplication. Operation stops like Dividing by Infinityresults in 0

    Output

    Dividing a finite number by 0 results in Infinity

    Output

    Some operations are incorrect by the rules of Mathematics. Like, We can't divide infinite numbers and also we cannot determine if an infinite number is even or odd, so if we try to find out the mod of infinite number we will get NaN (Not a Number)

    Output

Creating Infinity in JavaScript

There are several ways to create JavaScript Infinity.

  1. Infinity can be directly accessed by the global variable

    Output

  2. We can use Number.POSITIVE_INFINITY which is a property on the Number object

    Output

  3. If a big calculation is performed then also JavaScript Infinity can be produced

    Output

  4. The very famous math expression to create Ian nfinity is to divide by 0

    Output

Now let's look at ways to generate Negative Infinity

  1. If we put the - sign in front of Infinity then we get Negative Infinity

    Output

  2. We can use numbers.NEGATIVE_INFINITY to generate negative infinity

    Output

  3. We can generate a very large negative number

    Output

  4. If we divide any number by -0 then we get Negative Infinity

    Output

Dividing by Infinity in JavaScript

Division by Infinity can be broken down into three simple rules:

  1. Positive zero is produced when a positive number is divided by Infinity.
  2. Negative zero is obtained by multiplying a positive number by a negative number or by a negative Infinity,
  3. The result of dividing Infinity by Infinity is NaN.

Now let's see the code examples of the Javascript Infinity division

Output

Infinity and Math in JavaScript

JavaScript Infinity behaves the same as our friend Infinity does in Maths.

  • Adding and Subtracting to Infinity results in Infinity
    Output:
  • Dividing and Multiplying results sometimes in Infinity and sometimes in NaN
    Output:

Infinity and NaN in JavaScript

NaN in JavaScript stands for Not-a-Number, ironically it is a number that is not legal.

Performing any operation with NaN results in a NaN. Infinity or any other number when added, subtracted, divided, or multiplied with NaN results in NaN.

Let's look at some code samples for NaN

Output:

Infinity and large numbers in JavaScript

Infinity JavaScript is a special value in JavaScript which is bigger than any number value.

The largest number value is returned by Number.MAX_VALUE and it is equal to 1.7976931348623157e+308

The largest safest integer to use is returned by Number.MAX_SAFE_INTEGER and it is 9007199254740991

Output

For handling arbitrarily large integers, there is another data type in JavaScript which is BigInt. It is a primitive type and is different from the primitive type number that Infinity belongs to. For that reason, BigInt and Infinity do not work together nicely.

Let's look at some operations of BigInt with code examples

Output

Output

RangeError: Division by zero

Checking for Infinity with ==, ===, and Object.is()

We can use == and === to compare values to Infinity.

Output

The == equality operator in JavaScript compares for equality after doing any necessary type conversions.

Output

The === operator is the same as the equality operator == but it does not perform any type conversion on its own.

There is one more operator Object.is(), this works the same way as strict equality ===

Output

Checking for Infinity with isFinite() vs. Number.isFinite()

There are two functions to check Infinity JavaScript, these are isFinite() and Number.isFinite() There is only a slight difference between the two:

The isFinite() method performs type coercion which means that it will try to convert the value to a JavaScript number type before checking to see if it is a finite number.

Whereas, the Number.isFinite() method will not perform any type of coercion.

Number.isFinite() is the preferred method because it does not perform any type of coercion.

Let's look at some code examples to better understand the two methods

Output

Examples

Let's look at the different types of code samples which will give a more clear understanding of JavaScript Infinity

Using Infinity

One use case where Infinity is put to practical use is finding the minimum value in an array.

Output

The negative infinity

Negative Infinity can be used to get the smallest value in a logic block.

Output

Pitfalls of JavaScript Infinity

It happens rarely that we work directly with JavaScript Infinity, but we can encounter Infinity in some strange and unexpected places. Let's look at some situations where Infinity can cause problems if not handled carefully.

parseFloat() function

Getting data from forms is very common on websites nowadays. Suppose you have a form where you want to take the age as input. Since the value of the form field is a string, you have to convert it into a number. For conversion there are two methods parseInt() and parseFloat()

Output

If parseInt is supplied with a string that cannot be converted into a number then it gives NaN as result. Note that passing "Infinity" as a string also returns NaN.

But, if we used parseFloat() then,

Output

parseFloat() return Infinity when it gets the "Infinity" string as input.

JSON serialization

JSON.stringify() method serializes an infinite number to null

Output

Math methods

The biggest float number in JavaScript is Number.MAX_VALUE. If we try to use a number bigger than that then we get Infinity

Output

Math. max() and Math. min() are the two Math methods when invoked without arguments resulting in -Infinity and Infinity respectively.

Output

Conclusion

  • Infinity represents an infinite number.
  • Infinity is greater than any number while -Infinity is smaller than any finite number.
  • JavaScript Infinity follows the same rules as the Infinity in maths.
  • When numbers larger than Number.MAX_VALUE is used then it results in Infinity.
  • Comparison with Infinity can be done with the help of ==, ===, Object. is().
  • isFinite() methods perform type coercion while Number.isFinite() does not perform any type coercion.
  • There are some pitfalls to take care of with Infinity
    • parseFloat() returns Infinity if it is given the string "Infinity" as input.
    • JSON.stringify() method makes Infinity null.
    • Math methods like Math. max () and Math .min() return -Infinity and Infinity when given empty array as input.