The length of an array is its size, i.e. the number of elements in it. length is the index of the last element+1.
When we declared the weekday array previously, we declared an array of length 7:
var weekday = new Array(7);The array could have been declared without specifying length:
var weekday = new Array();Initially, the length would then have been 0, but the length would increase as elements were added. And as explained in the previous parts of the documentation, the length will always be 1 more than the last index of the array, as the indexes start with 0. This is true even if there are empty elements in it.
It is also possible to declare multidimensional arrays and arrays of arrays.
Length Property
As previously mentioned, untyped arrays are implemented as objects. Objects are a complex data type with collections of data that have properties and may be accessed via methods. A property returns a value that identifies an aspect of the state of an object. Methods are used to read or modify the data contained in an object.
We will get back to objects in general in Objects, and also the methods supported by the array object (go to About the Array Object for more information).
The length of an array is a property of an array. Object properties are accessed by appending a period and the name of the property to the name of the object:
objectName.propertyNameThe length of an array is determined as follows:
arrayName.lengthSo if we used this in the weekday example (before adding "Lastday"),
weekday.lengthwould return 7.