JavaScript Infinity

Summary: in this tutorial, you’ll learn about JavaScript Infinity and its features.

Introduction to the JavaScript Infinity

The Infinity is a property of the global object. In other words, it’s a global variable.

The initial value of the Infinity is Number.POSITIVE_INFINITY, which is greater than other finite numbers.

JavaScript also has a negative Infinity (-Infinity) where its value is Number.NEGATIVE_INFINITY. The -Infinity is smaller than any finite number.

The type of the Infinity is number :

const result = typeof (Infinity);

console.log(result); // numberCode language: JavaScript (javascript)

Checking for Infinity

JavaScript provides the Number.isFinite() that checks if a value is finite:

Number.isFinite(value);Code language: JavaScript (javascript)

It returns true if the value is finite; In case the value is infinite, the Number.isFinite() returns false.

console.log(Number.isFinite(100)); // true
console.log(Number.isFinite(Infinity)); // false
console.log(Number.isFinite(-Infinity)); // falseCode language: JavaScript (javascript)

Also, you can compare a value with Infinity like this:

let counter = 100;
console.log(counter === Infinity); // falseCode language: JavaScript (javascript)

And an infinite number equals another infinite number:

console.log(Infinity === Infinity); // trueCode language: JavaScript (javascript)

Pitfalls of Infinity

1) parseFloat() function

Suppose you have a form that collects user information. On the form, you have a field that allows users to enter their weight.

Since the value of the form field is a string, you need to convert it to a number:

let weight = parseInt('120kg');
console.log(weight); // 120Code language: JavaScript (javascript)

This is working fine. If users enter a string that cannot be converted to a number, the parseInt() returns a NaN:

let weight = parseInt('More than 100kg');
console.log(weight); // NaNCode language: JavaScript (javascript)

If the input string is Infinity, the parseInt() also returns NaN because it doesn’t recognize the infinite number:

let weight = parseInt('Infinity');
console.log(weight); // NaNCode language: JavaScript (javascript)

But if you use the parseFloat() function like this:

let weight = parseFloat('Infinity');
console.log(weight); // InfinityCode language: JavaScript (javascript)

…the parseFloat() function recognizes the Infinity and returns an infinite number.

So it’s a good practice to issue a validation error when an input field has the 'Infinity' string.

2) JSON serialization

The JSON.stringify() serializes an infinite number to null. For example:

const resource = {
    amount: Infinity
};
let result = JSON.stringify(resource);

console.log(result);Code language: JavaScript (javascript)

Output:

{"amount":null}Code language: JSON / JSON with Comments (json)

3) Math methods

Some Math functions return an infinite number.

The following Math.min() method returns the smallest numbers in the ratings array:

let ratings = [1, 2, 3, 4, 5];

let max = Math.min(...ratings);
console.log(max); // 1Code language: JavaScript (javascript)

However, when the ratings array is empty, the Math.min() method returns Infinity:

let ratings = [1, 2, 3, 4, 5];

// 
somewhere in the code 
ratings = []; // empty array
//

let max = Math.min(...ratings);
console.log(max); // InfinityCode language: JavaScript (javascript)

Summary

  • The Infinity represents an infinite number. It’s a global variable whose value is Infinity.
  • The Infinity is greater than any finite number while the -Infinity is smaller than any finite number.
  • The parseFloat('Infinity') returns Infinity.
Was this tutorial helpful ?