Open In App

JavaScript RangeError – Repeat count must be less than infinity

Last Updated : 24 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This JavaScript exception repeat count must be less than infinity occurs if the passed argument of String.prototype.repeat() method is infinity.

Output message:

RangeError: argument out of range (Edge)
RangeError: repeat count must be less than infinity and not 
            overflow maximum string size (Firefox)
RangeError: Invalid count value (Chrome)

Error Type:

RangeError

Cause of the error: The count parameter of the String.prototype.repeat() method passed is either less than 0 or greater than infinity.

Example 1: In this example, the parameter passed is 4, So the error has not occurred.

HTML




<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <p>
        JavaScript RangeError
        Repeat count must be less than infinity
    </p>
  
    <button onclick="Geeks();">
        click here
    </button>
  
    <p id="GFG_DOWN"></p>
  
    <script>
        var el_down = document.getElementById("GFG_DOWN");
          
        function Geeks() {
            try {
                "GFG".repeat(4);
                el_down.innerHTML = "'Repeat count " +
                  "must be less than infinity' " +
                  "error has not occurred";
            } catch (e) {
                el_down.innerHTML = "'Repeat count " +
                  "must be less than infinity' " + 
                  "error has occurred";
            }
        }
    </script>
</body>


Output:

JavaScript RangeError - Repeat count must be less than infinity

Example 2: In this example, the parameter passed is 232, So the error has occurred,

HTML




<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <p>
        JavaScript RangeError
        Repeat count must be less than infinity
    </p>
  
    <button onclick="Geeks();">
        click here
    </button>
  
    <p id="GFG_DOWN"></p>
  
    <script>
        var el_down = document.getElementById("GFG_DOWN");
        function Geeks() {
            try {
                "GFG".repeat(2 ** 32);
                el_down.innerHTML = "'Repeat count " +
                  "must be less than infinity' " + 
                  "error has not occurred";
            } catch (e) {
                el_down.innerHTML = "'Repeat count " +
                  "must be less than infinity' " + 
                  "error has occurred";
            }
        }
    </script>
</body>


Output:

JavaScript RangeError - Repeat count must be less than infinity



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads