JavaScript Array Object Length property

Source: Internet
Author: User
Tags javascript array

The length property can set or return the number of elements in the array.

Grammar:
Arrayobject.length


The length property 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 begin with element 0, the attribute length declares the number of elements in the array.

The length property of an array is initialized when an array is created with the constructor array (). When you add a new element to an array, the value of length is updated if necessary.

Set the Length property to change the size of the array. If you set a value that is smaller than its current value, the array is truncated and the elements at its tail are lost. If the value is set larger than its current value, the array will grow, and the new elements are added to the end of the array, their values are 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

JavaScript Array Object Length property

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.