Name
Array.length — the size of an array
Synopsis
array.length
Description
The length property of an
array is always one larger than the index of the highest element
defined in the array. For traditional “dense” arrays that have
contiguous elements and begin with element 0, the length property specifies the number of
elements in the array.
The length property of an
array is initialized when the array is created with the Array() constructor method. Adding new
elements to an array updates the length, if necessary:
a=newArray();// a.length initialized to 0b=newArray(10);// b.length initialized to 10c=newArray("one","two","three");// c.length initialized to 3c[3]="four";// c.length updated to 4c[10]="blastoff";// c.length becomes 11
You can set the value of the length property to change the size of an
array. If you set length to be
smaller than its previous value, the array is truncated, and
elements at the end are lost. If you set length to be larger than its previous
value, the array becomes bigger, and the new elements added at the
end of the array have the undefined value.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access