How to print positive and negative infinity values in JavaScript?

In JavaScript, infinity values represent numbers that exceed the maximum finite value. JavaScript provides built-in constants to handle positive and negative infinity values.

JavaScript Infinity Constants

JavaScript has two built-in constants for infinity values:

  • Infinity or Number.POSITIVE_INFINITY - represents positive infinity
  • -Infinity or Number.NEGATIVE_INFINITY - represents negative infinity

Basic Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Infinity Values</title>
</head>
<body>
    <h1>JavaScript Infinity Values</h1>
    <div id="output"></div>
    
    <script>
        let output = document.getElementById('output');
        
        // Display infinity values
        output.innerHTML = `
            <p>Positive Infinity: ${Infinity}</p>
            <p>Negative Infinity: ${-Infinity}</p>
            <p>Number.POSITIVE_INFINITY: ${Number.POSITIVE_INFINITY}</p>
            <p>Number.NEGATIVE_INFINITY: ${Number.NEGATIVE_INFINITY}</p>
        `;
    </script>
</body>
</html>

Output

Positive Infinity: Infinity
Negative Infinity: -Infinity
Number.POSITIVE_INFINITY: Infinity
Number.NEGATIVE_INFINITY: -Infinity

How Infinity Values are Created

Infinity values can occur in several ways:

<!DOCTYPE html>
<html>
<head>
    <title>Creating Infinity Values</title>
</head>
<body>
    <div id="results"></div>
    
    <script>
        let results = document.getElementById('results');
        let output = '';
        
        // Division by zero
        output += `<p>1 / 0 = ${1 / 0}</p>`;
        output += `<p>-1 / 0 = ${-1 / 0}</p>`;
        
        // Number too large
        output += `<p>1.8e308 = ${1.8e308}</p>`;
        output += `<p>-1.8e308 = ${-1.8e308}</p>`;
        
        // Math operations
        output += `<p>Math.pow(10, 1000) = ${Math.pow(10, 1000)}</p>`;
        
        results.innerHTML = output;
    </script>
</body>
</html>

Output

1 / 0 = Infinity
-1 / 0 = -Infinity
1.8e308 = Infinity
-1.8e308 = -Infinity
Math.pow(10, 1000) = Infinity

Checking for Infinity Values

You can check if a value is infinity using built-in methods:

<!DOCTYPE html>
<html>
<head>
    <title>Checking Infinity Values</title>
</head>
<body>
    <div id="check-results"></div>
    
    <script>
        let checkResults = document.getElementById('check-results');
        let output = '';
        
        let values = [Infinity, -Infinity, 100, NaN];
        
        values.forEach(value => {
            output += `<p>Value: ${value}</p>`;
            output += `<p>isFinite(${value}): ${isFinite(value)}</p>`;
            output += `<p>Number.isFinite(${value}): ${Number.isFinite(value)}</p>`;
            output += `<p>${value} === Infinity: ${value === Infinity}</p>`;
            output += `<hr>`;
        });
        
        checkResults.innerHTML = output;
    </script>
</body>
</html>

Practical Example with Large Number Operations

<!DOCTYPE html>
<html>
<head>
    <title>Infinity in Calculations</title>
    <style>
        body { font-family: Arial, sans-serif; }
        .result { margin: 10px 0; padding: 10px; background: #f0f0f0; }
        button { padding: 10px 20px; margin: 5px; }
    </style>
</head>
<body>
    <h1>JavaScript Infinity in Calculations</h1>
    <div class="result">
        <p>Large Number 1: <span id="num1">1.797693134862315E+308</span></p>
        <p>Large Number 2: <span id="num2">-1.797693134862315E+308</span></p>
    </div>
    
    <button onclick="addToNumbers()">Add 1 to Both Numbers</button>
    <button onclick="resetNumbers()">Reset</button>
    
    <div id="explanation"></div>
    
    <script>
        let num1Element = document.getElementById('num1');
        let num2Element = document.getElementById('num2');
        let explanation = document.getElementById('explanation');
        
        function addToNumbers() {
            let currentNum1 = parseFloat(num1Element.textContent);
            let currentNum2 = parseFloat(num2Element.textContent);
            
            let newNum1 = currentNum1 + 1;
            let newNum2 = currentNum2 - 1;
            
            num1Element.textContent = newNum1;
            num2Element.textContent = newNum2;
            
            explanation.innerHTML = `
                <div class="result">
                    <h3>Result:</h3>
                    <p>Adding 1 to ${currentNum1}: ${newNum1}</p>
                    <p>Subtracting 1 from ${currentNum2}: ${newNum2}</p>
                    <p><strong>Note:</strong> Numbers beyond JavaScript's max safe value become Infinity</p>
                </div>
            `;
        }
        
        function resetNumbers() {
            num1Element.textContent = '1.797693134862315E+308';
            num2Element.textContent = '-1.797693134862315E+308';
            explanation.innerHTML = '';
        }
    </script>
</body>
</html>

Key Points

  • JavaScript uses Infinity and -Infinity to represent infinite values
  • Division by zero results in infinity, not an error
  • Numbers larger than Number.MAX_VALUE become infinity
  • Use isFinite() to check if a number is not infinity
  • Infinity values can be used in mathematical operations

Conclusion

JavaScript handles infinity values gracefully using built-in constants. Understanding how infinity works is important for handling edge cases in mathematical calculations and avoiding unexpected results in your applications.

Updated on: 2026-03-15T23:18:59+05:30

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements