Sorting out javascript Array length issues

Source: Internet
Author: User
Tags javascript array

In javascript, the array length is very simple. You can use the length function to obtain the specific array length. However, when using the group length traversal function, it should be length-1 and the starting value is 0.

Array length:

You do not need to set the length of the javascript array. 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) // you can display the array length.

The above is not our focus. Let's take a look at it below.

First:

The Code is as follows: Copy code

Var lennon = Array ();
Lennon ["name"] = "windy ";
Lennon ["year"] = "1989 ";
Lennon ["living"] = true;
Alert (lennon. length );

Result: lennon. length is 0.



Cause:
Add an array element without changing the length of the array.
Arr ["a"] = 3;
Second point:

The Code is as follows: Copy code

Var foo2 = [];
Foo2 ['3'] = 3;
Foo2 ['age'] = 20;
Debugger;
Alert (foo2.length );


Foo2.length is 4 again

All array objects without assignment will be defined as undefined.
JavaScript can automatically re-adjust the array size. This automatic size adjustment mechanism makes it easier to see out of bounds arrays and is more difficult to find out. Pay more attention to similar situations.

Related Article

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.