Open In App

JavaScript Array() Constructor

The Array() constructor is used to create Array objects and the array constructor can be called with or without a new keyword, both can create a new Array.

Syntax:



new Array(Value1, Value2, ...);
new Array(ArrayLength);

Array(Value1, Value2, ...);
Array(ArrayLength);

Parameters: 

 



Example 1: In this example, we will use the basic use of the Array() constructor.




// Using elements
const array1 = new Array(1, 2, 3, 4, 5)
  
// Using arrayLength
const array2 = new Array(10)
  
console.log(array1);
console.log(array2);

Output:

(5) [1, 2, 3, 4, 5]
(10) [empty × 10]

Example 2: In this example, we will use an array with a string value.




const language = new Array("HTML", "CSS", "Javascript");
  
console.log(language.length);
console.log(language[0]);
console.log(language[1]);
console.log(language[2]);

Output:

3
HTML
CSS
Javascript

We have a complete list of Javascript Array() methods, to check those please go through this JavaScript Array Reference article.

Supported Browsers:

Article Tags :