About JavaScript array length problem collation

Source: Internet
Author: User

Array Length:

The JavaScript array does not need to be set in length, it expands itself, and the array name. Length returns the number of elements

The code is as follows Copy Code

var arr=new Array ("Saab", "Volvo", "BMW")

Alert (arr.length)//can display array length

The above is not our focus, focus on everyone look below.

1th:

The code is as follows Copy Code

var lennon=array ();
lennon["name"]= "Windy";
lennon["Year"]= "1989";
lennon["Living"]=true;
alert (lennon.length);

Results: Lennon.length was 0



Reason:
Adding an array element in the following way will not change the length of the array
Arr["a"]=3;
2nd:

The code is as follows Copy Code

var foo2 = [];
foo2[' 3 '] = 3;
foo2[' age ' = 20;
Debugger
alert (foo2.length);


Foo2.length is another 4.

All of the array objects that are not assigned are defined as undefined
JavaScript can automatically resize an array, an automatic sizing mechanism that makes it easier to get out of bounds arrays (out of bounds array) and harder to spot. So pay more attention to a similar situation.

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.