SoFunction
Updated on 2025-03-01

Introduction to array features in JavaScript

Unlike Java language, arrays in JavaScript have three features:

 

1. No type. The members of an array can be of any type, and the same array can also be composed of many different types of members.
2. The length is variable. The length of an array can be dynamically changed, so there is no problem of out-of-bounds of array access in JavaScript.
3. Discontinuity. The positions of members in the array can be continuous (0, 1, 2, 3…) or discontinuous. Any array has an attribute called length. When the array members are continuous, the length value is consistent with the number of array members; when the array members are not continuous, the length value is greater than the number of array members. Compared with continuous arrays, the read and write performance of discontinuous arrays is worse.

 

experiment:


Copy the codeThe code is as follows:

var o = [42, "Sample Text", {x:88}];//JavaScript array is un-typed.
(o);//[42, "Sample Text", Object {x=88}]
o[3] = 27;//JavaScript array is dynamic.
(o);//[42, "Sample Text", Object {x=88}, 27]
o[5] = 99;//JavaScript array is sparse.
(o);//[42, "Sample Text", Object {x=88}, 27, undefined, 99]


As can be seen from the example above, for discontinuous arrays, JavaScript will return undefined when accessing the missing member. If the array is continuous, but one of its members is undefined, the result of accessing the array is the same:


Copy the codeThe code is as follows:

var a = [42, "Sample Text", {x:88}, 27, undefined, 99];
(a);//[42, "Sample Text", Object {x=88}, 27, undefined, 99]


The array is discontinuous and has missing members, which is the same as the array is continuous but has undefined members. In both cases, the result of accessing the array content is the same. But there are still some subtle differences between the two, mainly manifested in accessing the array key:


Copy the codeThe code is as follows:

(4 in o);//false
(4 in a);//true


It can be seen that although the results obtained by accessing the content in these two cases are the same, their internal mechanism is completely different: when the array is discontinuous, a certain member is missing, so when accessing the member, JavaScript returns undefined; when the array is continuous, all members exist, but the values ​​of some members are relatively special and are undefined.

From the above example, we can also see that the essence of arrays in JavaScript is just an object with numbers as keys, and there is no difference between ordinary key values ​​and objects. In fact, when reading and writing an array, JavaScript will try to convert the parameters to positive integers. If the conversion is successful, the array operation will be performed (automatically update the length property of the array). If it fails, the parameters will be converted into a string and then read and write to a normal object. Of course, in the implementation of the JavaScript interpreter, a lot of performance optimizations are made for the feature of arrays using numbers as keys. Therefore, in actual use, if the keys of the object are all numbers, directly using the array object will get more efficient results.

In the process of defining an array, JavaScript allows extra commas to appear, and also allows the missing array members between two commas:


Copy the codeThe code is as follows:

var x = [1,2,3,];//trailing comma will be omitted.
();//3
        
var y = [1,,3];//member can be missed.
(y);//[1, undefined, 3]
(1 in y);//false
();//3


For array creation, JavaScript supports four methods:

1. Use literals (such as bracket expressions in the above examples) to directly create an array object.
2. Use the Array() constructor and do not pass in any parameters. In this case, an empty array is created with the same effect as [].
3. Use the Array() constructor to pass in a positive integer as the length of the array. In this case, JavaScript will reserve the corresponding memory space to store this array. It is worth noting that the keys of the array are not defined at this time, that is, there are no members in the array. Its effect is equivalent to that of [,,,,]
4. Use the Array() constructor to pass in the members of the array.

experiment:


Copy the codeThe code is as follows:

var z = new Array(10);//pre-allocate memory, but no index is defined yet.
(3 in z);//false

var m = new Array(42, 33, 99, "test", {k:99});
(m);//[42, 33, 99, "test", Object {k=99}]


In the ECMAScript 5 standard, you can use () to determine whether an object is an array:
Copy the codeThe code is as follows:

([]);//true
({});//false