Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1028182 of RangeError: invalid array length

  • Revision slug: Web/JavaScript/Reference/Errors/Invalid_array_length
  • Revision title: RangeError: invalid array length
  • Revision id: 1028182
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment review: minor fixes only, added tags, cleared review flags, avoided long lines in code sample.

Revision Content

{{jsSidebar("Errors")}}

Message

RangeError: invalid array length (Firefox)
RangeError: Invalid array length (Chrome)
RangeError: Invalid array buffer length (Chrome)

Error type

{{jsxref("RangeError")}}

What went wrong?

You were either:

  • creating an {{jsxref("Array")}} or an {{jsxref("ArrayBuffer")}} which has a length which is either negative or larger or equal to 232, or
  • setting the {{jsxref("Array.length")}} property, to a value which is either negative or larger or equal to 232.

Why are Array and ArrayBuffer length limited? The length property of an Array or an ArrayBuffer is represented with an unsigned 32-bit integer, that can only store values which are in the range from 0 to 232-1.

If you are creating an Array, using the constructor, you probably want to use the literal notation instead, as the first argument is interpreted as the length of the Array.

Otherwise, you might want to clamp the length before setting the length property, or using it as argument of the constructor.

Examples

Invalid cases

new Array(Math.pow(2, 40))
new Array(-1)
new ArrayBuffer(Math.pow(2, 32))
new ArrayBuffer(-1)

let a = [];
a.length = a.length - 1;         // set -1 to the length property

let b = new Array(Math.pow(2, 32) - 1);
b.length = b.length + 1;         // set 2^32 to the length property

Valid cases

[ Math.pow(2, 40) ]                     // [ 1099511627776 ]
[ -1 ]                                  // [ -1 ]
new ArrayBuffer(Math.pow(2, 32) - 1)
new ArrayBuffer(0)

let a = [];
a.length = Math.max(0, a.length - 1);

let b = new Array(Math.pow(2, 32) - 1);
b.length = Math.min(0xffffffff, a.length + 1);   

// 0xffffffff is the hexadecimal notation for 2^32 - 1
// which can also be written as (-1 >>> 0)

See also

  • {{jsxref("Array")}}
  • {{jsxref("Array.length")}}
  • {{jsxref("ArrayBuffer")}}

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

<pre class="syntaxbox">
RangeError: invalid array length (Firefox)
RangeError: Invalid array length (Chrome)
RangeError: Invalid array buffer length (Chrome)
</pre>

<h2 id="Error_type">Error type</h2>

<p>{{jsxref("RangeError")}}</p>

<h2 id="What_went_wrong">What went wrong?</h2>

<p>You were either:</p>

<ul>
 <li>creating an&nbsp;{{jsxref("Array")}} or an&nbsp;{{jsxref("ArrayBuffer")}} which has a length which is either negative or larger or equal to 2<sup>32</sup>, or</li>
 <li>setting the {{jsxref("Array.length")}} property, to a value which is either negative or larger or equal to 2<sup>32</sup>.</li>
</ul>

<p>Why are <code>Array</code> and <code>ArrayBuffer</code> length limited? The <code>length</code> property of an <code>Array</code> or an <code>ArrayBuffer</code> is represented with an unsigned 32-bit integer, that can only store values which are in the range from 0 to 2<sup>32</sup>-1.</p>

<p>If you are creating an <code>Array</code>, using the constructor, you probably want to use the literal notation instead, as the first argument is interpreted as the length of the <code>Array</code>.</p>

<p>Otherwise, you might want to clamp the length before setting the length property, or using it as argument of the constructor.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Invalid_cases">Invalid cases</h3>

<pre class="brush: js example-bad">
new Array(Math.pow(2, 40))
new Array(-1)
new ArrayBuffer(Math.pow(2, 32))
new ArrayBuffer(-1)

let a = [];
a.length = a.length - 1;         // set -1 to the length property

let b = new Array(Math.pow(2, 32) - 1);
b.length = b.length + 1;         // set 2^32 to the length property
</pre>

<h3 id="Valid_cases">Valid cases</h3>

<pre class="brush: js example-good">
[ Math.pow(2, 40) ]                     // [ 1099511627776 ]
[ -1 ]                                  // [ -1 ]
new ArrayBuffer(Math.pow(2, 32) - 1)
new ArrayBuffer(0)

let a = [];
a.length = Math.max(0, a.length - 1);

let b = new Array(Math.pow(2, 32) - 1);
b.length = Math.min(0xffffffff, a.length + 1);   

// 0xffffffff is the hexadecimal notation for 2^32 - 1
// which can also be written as (-1 &gt;&gt;&gt; 0)
</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Array")}}</li>
 <li>{{jsxref("Array.length")}}</li>
 <li>{{jsxref("ArrayBuffer")}}</li>
</ul>
Revert to this revision