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

How to create an empty array in JavaScript

青灯夜游
Release: 2021-10-20 17:40:05
Original
16961 people have browsed it

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 数组名=[值列表];
Copy after login

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

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

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(值列表);
Copy after login

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

var arr = new Array();  
console.log(arr);
Copy after login

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

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!

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