Home >Web Front-end >JS Tutorial >Briefly describe how javascript creates an array

Briefly describe how javascript creates an array

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-06-10 15:20:214322browse

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>

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!

Statement:
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