How to Fix “Invalid array length” in JavaScript
RangeError: Invalid array length is thrown when you pass a negative number, a decimal, Infinity, NaN, or a number greater than 232 – 1 to new Array() or to an array's length. It happens, for example, when page numbers for a list screen are built from "total items ÷ items per page" and an input field is empty or contains 0 or a negative number. If you check that the source values are usable numbers before computing the length, the error no longer occurs.
In my test environment, I built a page that lists page numbers from a total item count and items per page, then emptied an input field to trigger this error. I copied the message exactly as it appeared, and confirmed that the error disappears after the fix.
The test environment was Ubuntu 24.04 with Node.js 22.23.5. The page was served with Python 3.11's http.server. The browser was Chromium 158, launched with Playwright 1.63.0. Input and button clicks were done with Playwright, and errors were retrieved through the DevTools protocol. I also took screenshots of the DevTools panel. I did not test Firefox or Safari, nor errors from the length of ArrayBuffer or TypedArray.
Emptying the items-per-page input causes the error
The screen has input fields for "total items" and "items per page", plus a button.
<label>件数 <input type="number" id="total" value="25"></label>
<label>1ページの件数 <input type="number" id="per-page" value="10"></label>
<button id="make">ページ番号を作る</button>
<p id="pages">ページ:</p>
<script type="module" src="main.js"></script>
In main.js, the page count is computed as Math.ceil(total items / items per page), and the numbers 1, 2, 3… are built from an array of that length.
const button = document.getElementById('make');
const pages = document.getElementById('pages');
button.addEventListener('click', () => {
const total = Number(document.getElementById('total').value);
const perPage = Number(document.getElementById('per-page').value);
const pageCount = Math.ceil(total / perPage);
const numbers = [...new Array(pageCount).keys()].map((i) => i + 1);
pages.textContent = 'ページ:' + numbers.join(' ');
});
With 25 total items and 10 items per page, the screen showed the message meaning "Pages: 1 2 3". However, when I emptied the items-per-page input and pressed the button, the screen stayed as "Pages:" (the Japanese label alone, with no numbers), and the following appeared in the DevTools Console panel.
Uncaught RangeError: Invalid array length
at HTMLButtonElement.<anonymous> (main.js:8:23)

main.js:8:23 means line 8, column 23 of main.js, which is the position of new in new Array(pageCount). (The count includes the two leading spaces.)
The same message appeared at the same position when I entered -10 for items per page, and when I set both total items and items per page to 0. It also appeared when I set total items to 5000000000 and items per page to 1.
Only non-negative integers are valid lengths
According to MDN's description of the Array() constructor, if you pass a single number to new Array(), that number becomes the array's length. If the number is not an integer, or is outside the range 0 to 232 – 1 (4294967295), a RangeError is thrown.
I used Node.js to check what pageCount was for the inputs above.
$ node -e "console.log(Number(''), 25 / 0, 0 / 0, 25 / -10, Math.ceil(25 / -10), 25 / 10, Math.ceil(25 / 10), 2 ** 32 - 1)"
0 Infinity NaN -2.5 -2 2.5 3 4294967295
An empty input field's value is the empty string '', and Number('') is 0. Dividing 25 by 0 gives pageCount as Infinity. With items per page at -10, Math.ceil(-2.5) is -2. With both values at 0, the result is NaN. With total items at 5000000000 and items per page at 1, the value exceeds 4294967295. None of these can be used as a length.
I also passed various values to new Array() in Node.js to verify this.
new Array(3).length → 3
new Array(0).length → 0
new Array(-1) → RangeError: Invalid array length
new Array(2.5) → RangeError: Invalid array length
new Array(NaN) → RangeError: Invalid array length
new Array(Infinity) → RangeError: Invalid array length
new Array(2 ** 32 - 1).length → 4294967295
new Array(2 ** 32) → RangeError: Invalid array length
Array(-1) → RangeError: Invalid array length
Writing Array(-1) without new produces the same result. The message was Invalid array length, the same as in Chromium.
I also tested the case where I forgot Math.ceil and wrote const pageCount = total / perPage;. Even with normal inputs of 25 total items and 10 items per page, pageCount became 2.5, and the same Uncaught RangeError: Invalid array length appeared. Decimals are not valid lengths either.
Check the input before computing the length
Before computing the page count, I checked whether the total items and items per page were usable numbers. If not, the page shows a message and stops. To check, I used Number.isInteger(). It returns false for NaN and Infinity, so it rejects those along with non-integers.
const button = document.getElementById('make');
const pages = document.getElementById('pages');
const MAX_PAGES = 100;
button.addEventListener('click', () => {
const total = Number(document.getElementById('total').value);
const perPage = Number(document.getElementById('per-page').value);
if (!Number.isInteger(total) || total < 0) {
pages.textContent = '件数は0以上の整数で入れてください';
return;
}
if (!Number.isInteger(perPage) || perPage < 1) {
pages.textContent = '1ページの件数は1以上の整数で入れてください';
return;
}
const pageCount = Math.ceil(total / perPage);
if (pageCount > MAX_PAGES) {
pages.textContent = `ページが${MAX_PAGES}を超えるので、番号を並べません`;
return;
}
const numbers = [...new Array(pageCount).keys()].map((i) => i + 1);
pages.textContent = 'ページ:' + numbers.join(' ');
});
If items per page is at least 1, there is no division by zero, and pageCount is always a non-negative integer. For values that are too large, I stop at a limit well below 232 – 1, one that can actually be displayed as a list of numbers. Here the limit is 100 pages.
After the fix, emptying the items-per-page input and pressing the button produced no error. The screen showed a message meaning "Items per page must be an integer of 1 or more," and nothing appeared in the DevTools Console panel.

With -10 or 2.5 for items per page, or with both values at 0, the same message appeared. With total items at 5000000000 and items per page at 1, the screen showed a message meaning "There are more than 100 pages, so no numbers are shown." With total items 25 and items per page 10, the output was the same as before the fix: "Pages: 1 2 3".
Switching to Array.from does not always remove the error
I replaced the array construction with Array.from({ length: pageCount }, (_, i) => i + 1) and tested it without checking the input. The result depended on the input.
- Items per page empty (
pageCountisInfinity):Uncaught RangeError: Invalid array lengthappeared. The second line wasat Array.from (<anonymous>). - Total items
5000000000, items per page1: the same error appeared. - Items per page
-10(pageCountis-2): no error, and the screen stayed as "Pages:". - Both values
0(pageCountisNaN): no error, and the screen stayed as "Pages:".
According to MDN's section on normalization of the length property in the Array description, the length of an object such as { length: … } is converted to an integer and clamped to the range 0 to 253 – 1. NaN becomes 0. In Node.js, Array.from({ length: -2 }) and Array.from({ length: NaN }) produced arrays of length 0, and Array.from({ length: 2.5 }) produced an array of length 2. Only Array.from({ length: Infinity }) threw RangeError: Invalid array length.
According to MDN's description of Array.from(), Array.from creates the array using the normalized length. Infinity is clamped to 253 – 1, but that is still above the maximum array length of 232 – 1, so the error is thrown.
Because negative numbers and NaN do not throw, it is easier to miss that the input is wrong. Even when using Array.from, you still need to validate the input.
The same error occurs when assigning to length
According to MDN's description of length, you can assign a value to an array's length, and making it shorter removes the trailing elements. Assigning a negative number or a decimal here also throws the same error. These are the results from Node.js.
const a = []; a.length -= 1 → RangeError: Invalid array length
const a = [1, 2]; a.length = 2.5 → RangeError: Invalid array length
const a = []; a.length = Math.max(0, a.length - 1) → 0
Subtracting 1 from the length of an empty array gives -1, which throws. Clamping the value so it is never below 0 with Math.max(0, …), as shown in MDN's explanation of this error, removed the error.
The same page lists the message for each browser. V8-based browsers and Firefox show RangeError: invalid array length, and Safari shows RangeError: Array size is not a small enough positive integer. MDN writes the message with a lowercase "i", but in my test environment, Chromium 158 and Node.js 22 showed Invalid array length with an uppercase "I". If you see the Safari message, suspect the same cause.




