Javascript Array object length attribute, arraylength
The length attribute can be set or the number of elements in the array is returned.
Syntax:
ArrayObject. length
The length attribute of an array is always 1 larger than the subscript of the last element defined in the array. For regular arrays that have continuous elements and start with element 0, the attribute length declares the number of elements in the array.
The length attribute of the Array is initialized when an Array is created using the constructor Array. When adding a new element to an array, the length value is updated if necessary.
Setting the length attribute can change the size of the array. If the value is smaller than its current value, the array is truncated and the elements at the end of the array are lost. If the set value is greater than the current value, the array will increase, and new elements will be added to the end of the array. Their values will be undefined.
Example:
<Script type = "text/javascript"> var arr = new Array (3) arr [0] = "John" arr [1] = "Andy" arr [2] = "Wendy" document. write ("Original length:" + arr. length) document. write ("<br/>") arr. length = 5document. write ("New length:" + arr. length) </script>
Print:
Original length: 3
New length: 5