Home  >  Article  >  Web Front-end  >  How to create an empty array in JavaScript

How to create an empty array in JavaScript

青灯夜游
青灯夜游Original
2021-10-20 17:40:0516960browse

How to create an empty array in JavaScript: 1. Use the array direct value "[]", the syntax "var array name=[];"; 2. Use the Array() function, the syntax "var array name=new Array();" or "var array name=new Array(array length value);".

How to create an empty array in JavaScript

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

Method 1: Use array literal "[]"

The syntax format of array literal: include multiple values ​​in square brackets List, values ​​separated by commas.

var 数组名=[值列表];

If the value list is empty, an empty array can be created.

var arr = [];  //空数组
console.log(arr);

How to create an empty array in JavaScript

Method 2: Using the Array() function

When using the new operator to call the Array() type function, you can construct a new array.

var 数组名 = new Array(值列表);

Similarly if the value list is empty, an empty array can be created.

var arr = new Array();  
console.log(arr);

How to create an empty array in JavaScript

If a numeric parameter is passed to Array(), the parameter value is equal to the attribute value of the array length, which can define the length of the array, that is, the number of elements it contains.

var arr = new Array(5);  //有指定长度的空数组
console.log(arr);

How to create an empty array in JavaScript

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to create an empty array in JavaScript. 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