The study of array string index and numerical index in JS

Source: Internet
Author: User

Let's look at a question first:

varArray =[];array["a"] ="Hello"; array["b"] =" World"; array["C"] ="Yes"; Console.log ("Length:"+ Array.Length);//0

In many of the JavaScript books that I've seen before, it's claimed that JS can use strings as an array subscript, so I didn't feel anything wrong when I was using them. But when we look at the console output of the last sentence, the unexpected situation arises: length is 0

The length value of the displayed array is actually zero, and then the previous step is tested to see if the sequence was successful:

The array is printed normally, and the value values inside the array are correctly taken from the string index

proves that the previous operation was successful, since the array's subscript can be a string, why is the length of the array displayed as zero after inserting the element?

To further explore this issue, I made a little change to the above code:

Now the value of length becomes 21. The array is equivalent to 20 empty elements, plus an element of index 20 value 21, followed by three key elements a, B, c

In order to find out, I changed the index to string 100, printing the entire array of information, the output is as follows:,

This allows you to see information about all the elements I have added to the array, but so on ... This kind of display is a bit familiar, it does not look like a normal array of display, but rather like an object!

We know that the array object is inherited from object objects, so the "invisible" array element becomes the property of the current array instance object?

  for this question, Michael Berkowski The Great God explains this :

the array of JavaScript actually does not have a real string subscript like PHP or some other language, when we try to give a . Because a property is added to an array object , which is the same length as the property of the object, so the value will always be zero. Similarly, the. Pop () and. Shift () methods that act on an array element are also not capable of acting on these object properties. Therefore, if you want to use an array that consists entirely of a string subscript, it is better to declare it as an object of type objects.

But here comes the new question, since the element with the string subscript will be used as the property of the array object, so why array["10"] this item will cause the value of length to become 11? I found the following article: "JS array subscript summary " http://www.phper.org.cn/?post=65, in the first sentence of the article answered this question. Based on the results obtained after a series of discussions, the answers to the above questions are organized as follows:

 The range of subscript values for JavaScript arrays is from 0 to 2 of 32 parties .

For any given numeric subscript value, if not within this range, JS converts it to a string and values the subscript as a property value of the array object instead of an array element , for example array[-1] = "Yes" In fact, it is equivalent to adding a property named-1 to the array object with the property value Yes.

If the subscript value is within the legal range, then whether the subscript value is a number or a numeric string, it will be converted to a numeric use, i.e. array["100"] = 0 and array[100] = 0 Perform the same operation.

The study of array string index and numerical index in JS

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.