Home > Web Front-end > JS Tutorial > body text

Briefly describe how javascript creates an array

醉折花枝作酒筹
Release: 2021-06-10 15:20:21
Original
4231 people have browsed it

In js, you can use the array method to create an array. The specific method is: 1. Create an array through the constructor. The syntax format is "let array name=new Array();"; 2. Create through literals. Array, the syntax format is "let array name=[];".

Briefly describe how javascript creates an array

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

1. Create an array through the constructor

Create an array of a specified size

let array name = new Array (array length) ;

Create an empty array

let array name = new Array();

Create an array of given data

let array name = new Array(data1, data2, data3...);

2. Create an array through literals

Create an empty array

let array name = [];

Create an array of given data

let array name = [data1, data2, data3...];

Sample code

	<script>
	        //通过构造函数创建数组
	        let arr1 = new Array(3);
	        arr1[0] = 11;
	        arr1[1] = 12;
	        arr1[2] = 13;
	        let arr2 = new Array();
	        arr2[0] = 20;
	        arr2[1] = 21;
	        arr2[2] = 22;
	        let arr3 = new Array(31, 32, 33);
	        console.log(arr1);
	        console.log(arr2);
	        console.log(arr3);
	        
	        //通过字面量创建数组
	        let arr4 = [];
	        arr4[0] = 41;
	        arr4[1] = 42;
	        arr4[2] = 43;
	        let arr5 = [51, 52, 53];
	        console.log(arr4);
	        console.log(arr5);
	    </script>
Copy after login

Running effect

Briefly describe how javascript creates an array

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of Briefly describe how javascript creates an array. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!