Javascript Array object length attribute, arraylength

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.