Home >Web Front-end >JS Tutorial >How to create an empty array in JavaScript
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);".
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);
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);
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);
[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!