JavaScript

# What is the Infinity Property Used for in JavaScript

A unique value in JavaScript called “Infinity” is used to represent mathematical infinity () and overflow values or uncountable numbers. More than any finite integer, infinity is larger. When a number crosses the highest limit for a number 1.797693134862315e+308, it hits infinity.

This tutorial will describe the infinity property in JavaScript.

## What is the Infinity Property Used for in JavaScript?

JavaScript Infinity is a numeric value that can be assigned to any variable in the same manner that other numbers can be assigned to variables.

When you add any number in Infinity by assigning it to any variable, it outputs “Infinity”:

var sum = Infinity + 18

Output The Infinity is a global object that has the attribute of infinite length. Technically, Infinity is categorized as a Window object property: Of course, infinity is not the same as other numbers; it is greater than any finite number. Let’s see the below-given GIF, where it can be observed that when “1” exceeds the length of 308, it gives an Infinity: ## Classification of Infinity in JavaScript

Infinity is classified as positive Infinity “+Infinity” and negative Infinity “-Infinity”. If the number exceeds 1e308, it will give Infinity or +Infinity, while if the negative number exceeds -1e308, it gives -Infinity:  Let’s see how the Infinity works in the arithmetical operations.

## How Does Infinity Property Work With Arithmetical Operations?

Take 10 power of 1000 which will output Infinity because the returned value exceeds the limit:

const infiniteNumber = Math.pow(10, 1000);

Output If you will divide any finite number with zero, it will also return Infinity:

const division = 1/0;

Output Sometimes, when you use infinity with any finite number, such as divide any number with infinity, it gives a finite number “0”;

const division = 100/Infinity;

Output Infinity is not used as an iterable object, such as a “for-of” loop, if you try to utilize it, it will give an error “Uncaught TypeError: Infinity is not iterable”: ## Bonus Tip

Infinity is used in a “for” loop, but it is not recommended, as it creates an infinite loop. Sometimes, the browser warns that the script has an infinite loop and will attempt to escape it. While most of the time, it crashes the browser:

for (let i=0; i<Infinity; i++) {
// Infinite loop
}

That’s all about the Infinity property used in JavaScript.

## Conclusion

Infinity is a unique numeric value in JavaScript that is greater than any finite number. Infinity is classified as positive Infinity “+Infinity” and negative Infinity “-Infinity”. If the number exceeds 1e308, it will give Infinity or +Infinity, while if the negative number exceeds -1e308, it outputs -Infinity. It is used in arithmetic operations and conditional statements but not used as an iterable object. This tutorial described the JavaScript infinity property. 