Home > Web Front-end > Front-end Q&A > Can JavaScript arrays not be initialized in size?

Can JavaScript arrays not be initialized in size?

青灯夜游
Release: 2022-02-07 11:55:55
Original
2744 people have browsed it

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.

Can JavaScript arrays not be initialized in size?

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);
Copy after login

Can JavaScript arrays not be initialized in size?

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);
Copy after login

Can JavaScript arrays not be initialized in size?

【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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template