Last Updated : 12 Jul, 2024
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, ...);Parameters:
new Array(ArrayLength);Array(Value1, Value2, ...);
Array(ArrayLength);
Example 1: In this example, we will use the basic use of the Array() constructor.
JavaScript
// 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.
JavaScript
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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4