Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Array length: Array.length

The length property specifies an array's length, but not necessarily the number of existing elements. Assigning to it can delete elements or increase the length without filling the new positions.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

array.length
array.length = newLength

Array.length here denotes the length property of an array instance, read as array.length, not the property of the Array constructor. It is a property, not a method. Its value is an integer from 0 through 2 ** 32 - 1.

Length is not a count of existing elements: an array can contain empty slots. If an assignment creates an element at an array index equal to or greater than the current length, length increases to that index plus one. If length has been made non-writable, such an assignment does not add the element or extend the array: it throws a TypeError in strict mode and fails silently otherwise. delete array[i] removes the element if it is a configurable own property of the array, but does not reduce the length. If the own property at that index is non-configurable, it remains in place: delete returns false in non-strict code and throws a TypeError in strict code.

Assigning a smaller length deletes own elements whose indices are greater than or equal to the new length. Increasing the length does not create elements containing undefined; it leaves empty slots. In the example, writing at index 4 sets the length to 5, and later shortening to 2 removes that element.

If an assignment to array.length reaches length validation, a negative number, a non-integer fraction, NaN, Infinity, or 2 ** 32 causes a RangeError. If length is non-writable, the assignment is rejected without validating the new length: it throws a TypeError in strict mode and fails silently otherwise. Initially, length is writable, non-enumerable, and non-configurable. A non-configurable element can block truncation: the length stops at its index plus one; in strict mode that assignment throws a TypeError, and higher-indexed elements may already have been deleted.

Examples

const items = [10, 20];
items[4] = 50;
console.log(items.length);
console.log(Object.hasOwn(items, 2));
items.length = 2;
console.log(JSON.stringify(items));
items.length = 4;
console.log(items.length);
console.log(Object.hasOwn(items, 2));

Result

5
false
[10,20]
4
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources