Open In App

What is the Infinity Global Property in JavaScript ?

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, the Infinity global property represents positive infinity, which is a numeric value that is greater than any other numeric value. It can be used to represent values that are too large to be represented with the Number data type or as a result of operations like division by zero. Also,  the upper limit of the floating point number is 1.7976931348623157e+308 and the lower limit is -1.797693134862316E+308

Syntax:

Infinity

Return Value:

Infinity // For positive infinity
-Infinity // For negative infinity

Example 1: To demonstrate the use of the Infinity global property in mathematical operations such as Division by Zero,Multiplication with Infinity,Subtraction from Infinity.

Javascript
const result1 = 10 / 0;
console.log(result1);

const result2 = 1000 * Infinity;
console.log(result2);


const result3 = Infinity - 1000;
console.log(result3);

Output
Infinity
Infinity
Infinity

Example 2: To demonstrate the use of the Infinity global property in comparison such as Comparing Greater Than,Comparing Less Than,Comparing Equality.

Javascript
const number = 100;
console.log(number > Infinity);

console.log(number < Infinity);

console.log(number === Infinity);

console.log(Infinity === Infinity); 

Output
false
true
false
true

Example 3: To demonstrate the Infinity property that is used to represent the upper limit of floating-point numbers in JavaScript.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>JavaScript Infinity Property</title>
</head>

<body>
    <center>
        <h3>
            JavaScript Infinity Property
        </h3>
        <button onclick="test()">
            Click Here
        </button>
        <p>
            1.7976931348623157e+308 is the upper 
              limit of a Floating Point Number.
        </p>
        <h4>
            Clicking the button will showcase 
              the usage of the Infinity property.
        </h4>
        <p id="result"></p>
    </center>
    <script>
        function test() {
            const largeNumber = 1.7976931348623157e+309;


            document
                .getElementById("result")
                .innerHTML = 
                      "Attempting to represent a number beyond the upper limit: " + 
                      largeNumber;
        }
    </script>
</body>

</html>

Output:

Ht

the upper limit of floating-point numbers demonstration


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads