Friday, October 24, 2025
HomeLanguagesJavascriptJavaScript RangeError – Invalid array length

JavaScript RangeError – Invalid array length

This JavaScript exception Invalid array length occurs when creating an Array or an ArrayBuffer of the array length either negative or greater than or equal to 232. It can also occur if the length property is set manually to a value either negative or greater than or equal to 232.

Output message:

RangeError: Array length must be a finite positive integer (Edge)
RangeError: invalid array length (Firefox)
RangeError: Invalid array length (Chrome)
RangeError: Invalid array buffer length (Chrome)

Error Type:

RangeError

Cause of the error: The length of an Array or an ArrayBuffer can only be represented by an unsigned 32-bit integer, which only stores values ranging from 0 to 232-1. While creating an Array or an ArrayBuffer, if the array length is either negative or greater than or equal to 232  then this error occurs. 

Example 1: In this example, the length property is set to 6, which is a valid value, therefore no error occurred.

HTML




<head>
    <script src=
    </script>
</head>
<body style="text-align: center;">
    <h1 style="color: green;">
        neveropen
    </h1>
  
    <p>
        JavaScript RangeError
        Invalid array length
    </p>
  
    <button onclick="Geeks();">
        click here
    </button>
    <p id="GFG_DOWN"></p>
  
    <script>
        var el_down = document.getElementById("GFG_DOWN");
        function Geeks() {
            try {
                let a = [];
                a.length = 6;
                el_down.innerHTML = 
                  "'Invalid array length' "+
                  "error has not occurred";
            } catch (e) {
                
                // Show the error in console
                console.log(e);
                el_down.innerHTML = 
                  "'Invalid array length' "+
                  "error has occurred";
            }
        }
    </script>
</body>


Output:

JavaScript RangeError - Invalid array length

Example 2: In this example, the length property is set to -1, which is an invalid value, therefore the error has occurred.

HTML




<head>
    <script src=
    </script>
</head>
<body style="text-align: center;">
    <h1 style="color: green;">
        neveropen
    </h1>
  
    <p>
        JavaScript RangeError
        Invalid array length
    </p>
  
    <button onclick="Geeks();">
        click here
    </button>
    <p id="GFG_DOWN"></p>
  
    <script>
        var el_down = document.getElementById("GFG_DOWN");
        function Geeks() {
            try {
                let a = [];
                a.length = -1;
                el_down.innerHTML = 
                  "'Invalid array length' "+
                  "error has not occurred";
            } catch (e) {
                
                // Show the error in console
                console.log(e);
                el_down.innerHTML = 
                  "'Invalid array length' "+
                  "error has occurred";
            }
        }
    </script>
</body>


Output:

JavaScript RangeError - Invalid array length

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS