Skip to main content
Version: 3.27.2

Array.prototype.length

The length data property of an Array instance represents the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

Value

A non-negative integer less than 232.

Description

The value of the length property is a non-negative integer with a value less than 232.

const listA = [1, 2, 3];
const listB = new Array(6);

console.log(listA.length);
// 3

console.log(listB.length);
// 6

listB.length = 2 ** 32; // 4294967296
// RangeError: Invalid array length

const listC = new Array(-100); // Negative numbers are not allowed
// RangeError: Invalid array length

The array object observes the length property, and automatically syncs the length value with the array's content. This means:

  • Setting length to a value smaller than the current length truncates the array — elements beyond the new length are deleted.
  • Setting any array index (a non-negative integer smaller than 232) beyond the current length extends the array — the length property is increased to reflect the new highest index.
  • Setting length to an invalid value (e.g. a negative number or a non-integer) throws a RangeError exception.