JavaScript Array length property
property sets or returns the number of elements in an array

length property sets or returns the number of elements in an array.

length is a property in JavaScript, it does not take in any parameters. It is simply appended to the end of an array.

length property is always one higher than the highest index value in the array because index values start from 0 in JavaScript.

Setting length to a smaller value than the current length truncates the array; elements beyond the new length are deleted.

Setting length to a bigger value than the current length, the array is extended by adding empty slots, not actual undefined values ↴

const myArray = ['a', 'b', 'c'];

myArray.length = 6; returns 6

myArray returns ['a', 'b', 'c', empty x 3]

Setting length to zero, will return 0 and the array will be empty []

Setting length to an invalid value (e.g. a negative number or a non-integer) throws RangeError exception.

syntax 1 description
return the number of elements in myArray
myArray.length
syntax 2 description
set the number of elements in myArray
myArray.length = number