JavaScript (ES2026) reference · Array Methods

JavaScript Array length Property

The number of elements in the array. Setting it shortens or extends the array.

JavaScript (ES2026) Since ES1

Syntax

array.length

Return value

An integer: one more than the highest index.

Examples

Read and set the lengthTry it yourself
const fruits = ["apple", "banana", "cherry"];
console.log(fruits.length);
fruits.length = 1;   // truncate
console.log(fruits);

Output

3
[ 'apple' ]
Check for empty, get the last elementTry it yourself
const items = [];
if (items.length === 0) console.log("empty");
console.log([1, 2, 3][[1, 2, 3].length - 1]); // last element

Output

empty
3

Version history

VersionChange
JavaScript ES1Added

In other languages