Interesting JavaScript Array length problem Code Description _ javascript skills

Source: Internet
Author: User
Tags javascript array
Interesting JavaScript Array code example. For more information about js learning, see. Pay attention to the following situations. First:

The Code is as follows:


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:


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.