freeCodeCamp/guide/english/javascript/standard-objects/array/array-length/index.md

654 B

title
Array Length

Array Length

length is a property of arrays in JavaScript that returns or sets the number of elements in a given array.

The length property of an array can be returned like so.

let desserts = ["Cake", "Pie", "Brownies"];
console.log(desserts.length); // 3

The assignment operator, in conjunction with the length property, can be used to set then number of elements in an array like so.

let cars = ["Saab", "BMW", "Volvo"];
cars.length = 2;
console.log(cars.length); // 2

More Information:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length