The code copy is as follows:
var beatles = ["john","Paul","George","Ringo"];
The beatles array above is a typical example of a traditional array: the subscript of each element is a number, and for each element added, the number is increased by 1 in turn. The subscript of the first element is 0, and the subscript of the second element is 1. And so on.
If only the value of the element is given when filling the array, this array will be a traditional array, and the subscripts of its respective elements will be automatically created and refreshed.
This default behavior can be changed by explicitly giving subscripts for each new element when filling the array. When giving subscripts for new elements, you do not have to be limited to using integer numbers. You can also use strings:
The code copy is as follows:
var lennon = Array();
lennon["name"] = "John";
lennon["year"] = "1940";
lennon["living"] = false;
Such an array is called an associative array. Since strings can be used instead of numeric values, the code is more readable. However, this usage is not a good habit and is not recommended for everyone to use. Essentially, when creating an associative array, you create properties of the Array object. In JavaScript, all variables are actually objects of some type. For example, a Boolean value is an object of type Boolean, and an array is an object of type Array. In the example above, you actually added name, year and living are that attributes to the lennon array. Ideally, you should not modify the properties of an Array object, but use a common object.
The above is all about this article, I hope you like it.