Array.prototype.length

Holds the number of elements in an array, and truncates or extends it when assigned.

Signature
array.length
  • Added in JavaScript ECMAScript 1

Return value

numberA nonnegative integer, always one greater than the highest index in the array, between 0 and 2**32 - 1.

Errors

Throws a RangeError when assigned a value that is negative, fractional, or greater than or equal to 2**32.

length is an own property of every array instance, not an inherited accessor, and its attributes are writable: true, enumerable: false, configurable: false. The engine keeps it one greater than the highest integer index, so assigning to arr[99] on a three-element array makes length 100.

When to reach for assignment

Writing to length is the only way to shrink an array in place without splice(). arr.length = 0 empties an array that other code holds a reference to — reassigning arr = [] would leave those references pointing at the old contents. arr.length = n is a fast truncation to the first n elements.

What trips people up

  • Growing creates holes, not values. After arr.length = 5 on a two-element array, indexes 2 to 4 are empty slots that map() and forEach() skip, and 3 in arr is false.
  • length counts index positions, not defined elements: a sparse array reports the full span.
  • It counts UTF-16 positions for strings, so length does not equal the number of user-visible characters there.
  • Assigning a negative or fractional value throws a RangeError rather than coercing.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Empty an array in place with arr.length = 0 when other code holds the same reference.
  • Truncate a log or cache to its most recent n entries.
  • Check whether a list is empty before rendering a fallback.
  • Pre-size an array before calling fill() to seed it.

Frequently asked questions

How do I empty an array in JavaScript?

Set arr.length = 0 to clear it in place, which every reference to that array sees. Reassigning arr = [] only rebinds your variable and leaves other holders pointing at the original contents.

Why is my array length bigger than the number of elements?

Because length tracks the highest index plus one, not how many elements exist. Assigning arr[99] = 1 or setting length directly creates empty slots that count toward length but are skipped by forEach(), map() and filter().

What is the maximum length of a JavaScript array?

2**32 - 1, or 4,294,967,295 elements. Assigning a length of 2**32 or higher, or a negative or fractional value, throws a RangeError.

More examples

Clearing a shared array

Setting length to 0 empties the single underlying array, so both names see it empty. Reassigning only rebinds one name and leaves alias pointing at the old, still-populated array.

Example · javascript
const queue = ["a", "b", "c"];
const alsoQueue = queue;

queue.length = 0;
console.log(queue, alsoQueue);

let other = ["a", "b"];
const alias = other;
other = [];
console.log(other, alias);

Holes are not undefined

arr[3] reads as undefined but the property does not exist, so forEach visits only 2 elements and map leaves the holes in place. Spreading uses the iterator, which materialises them as real undefined values.

Example · javascript
const arr = [1, 2];
arr.length = 5;

console.log(arr.length);
console.log(arr[3], 3 in arr);

let visited = 0;
arr.forEach(() => visited++);
console.log("forEach visited", visited);
console.log(arr.map((v) => v * 2));
console.log([...arr]);

Invalid lengths throw

Negative, fractional and out-of-range values all raise a RangeError instead of being coerced, so the array keeps its original length of 3.

Example · javascript
const arr = [1, 2, 3];

for (const bad of [-1, 1.5, 2 ** 32]) {
  try {
    arr.length = bad;
  } catch (err) {
    console.log(bad, "->", err.constructor.name, err.message);
  }
}

console.log(arr.length);

Discussion

  • Be the first to comment on this lesson.