JavaScript arrays can be initialized in size; you only need to pass a numeric parameter to the Array() function when using the new operator to call the Array() type function to create an array, to initialize the array size and define the length of the array. That is, the number of elements it contains.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
There are two ways to define (create or declare) an array in JavaScript: constructing an array and array literal.
When using the new operator to call the Array() type function, you can construct a new array.
When you pass a numeric parameter to the Array() function, you can initialize the array size and define the length of the array, that is, the number of elements it contains.
Example:
var arr = new Array(5); //指定长度的数组 console.log(arr);
It can be seen that the initialization array size is defined as 5, and there are no array elements in it.
When passing multiple values, a real array can be created.
var arr = new Array(1, true, "string", [1,2], {x:1,y:2}); //实数组 console.log(arr);
【Related recommendations: javascript learning tutorial】
The above is the detailed content of Can JavaScript arrays not be initialized in size?. For more information, please follow other related articles on the PHP Chinese website!