Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

RangeError: invalid array length

The JavaScript exception "Invalid array length" occurs when specifying an array length that is either negative, a floating number or exceeds the maximum supported by the platform (i.e., when creating anArray orArrayBuffer, or when setting thelength property).

The maximum allowed array length depends on the platform, browser and browser version.ForArray the maximum length is 232-1.ForArrayBuffer the maximum is 231-1 (2GiB-1) on 32-bit systems.From Firefox version 89 the maximum value ofArrayBuffer is 233 (8GiB) on 64-bit systems.

Note:Array andArrayBuffer are independent data structures (the implementation of one does not affect the other).

Message

RangeError: invalid array length (V8-based & Firefox)RangeError: Array size is not a small enough positive integer. (Safari)RangeError: Invalid array buffer length (V8-based)RangeError: length too large (Safari)

Error type

What went wrong?

The error might appear when attempting to produce anArray orArrayBuffer with an invalid length, which includes:

  • Negative length, via the constructor or setting thelength property.
  • Non-integer length, via the constructor or setting thelength property. (TheArrayBuffer constructor coerces the length to an integer, but theArray constructor does not.)
  • Exceeding the maximum length supported by the platform. For arrays, the maximum length is 232-1. ForArrayBuffer, the maximum length is 231-1 (2GiB-1) on 32-bit systems, or 233 (8GiB) on 64-bit systems. This can happen via the constructor, setting thelength property, or array methods that implicitly set the length property (such aspush andconcat).

If you are creating anArray using the constructor, you probably want to use the literal notation instead, as the first argument is interpreted as the length of theArray. Otherwise, you might want to clamp the length before setting the length property, or using it as argument of the constructor.

Examples

Invalid cases

js
new Array(2 ** 40);new Array(-1);new ArrayBuffer(2 ** 32); // 32-bit systemnew ArrayBuffer(-1);const a = [];a.length -= 1; // set the length property to -1const b = new Array(2 ** 32 - 1);b.length += 1; // set the length property to 2^32b.length = 2.5; // set the length property to a floating-point numberconst c = new Array(2.5); // pass a floating-point number// Concurrent modification that accidentally grows the array infinitelyconst arr = [1, 2, 3];for (const e of arr) {  arr.push(e * 10);}

Valid cases

js
[2 ** 40]; // [ 1099511627776 ][-1]; // [ -1 ]new ArrayBuffer(2 ** 31 - 1);new ArrayBuffer(2 ** 33); // 64-bit systems after Firefox 89new ArrayBuffer(0);const a = [];a.length = Math.max(0, a.length - 1);const b = new Array(2 ** 32 - 1);b.length = Math.min(0xffffffff, b.length + 1);// 0xffffffff is the hexadecimal notation for 2^32 - 1// which can also be written as (-1 >>> 0)b.length = 3;const c = new Array(3);// Because array methods save the length before iterating, it is safe to grow// the array during iterationconst arr = [1, 2, 3];arr.forEach((e) => arr.push(e * 10));

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp