JavaScript new Array()
Examples
// Create a new Array
const cars = new Array("Saab", "Volvo", "BMW");
Try it Yourself »
const a = new Array();
const b = new Array(3);
const c = new Array("3");
const d = new Array("Saab", "Volvo", "BMW");
Try it Yourself »
More Examples Below !
Description
The new Array() constructor creates a new Array object.
Syntax
new Array()
new Array(number)
new Array(non-number)
new Array(element1, ..., elementN)
new Array(array literal)
Parameters
| Parameter | Description |
| none | Optional. Creates an empty array. |
| number | Optional. Creates an array with number of empty slots. |
|---|---|
| non-number | Optional. Creates an array with non-number as the only element. |
| element1, ..., elementN |
Optional. Comma separated values to create a new new array from. |
| array literal | Optional. Creates an array from an array literal. |
Parameters Explained
new Array() called with no arguments, creates an empty array.
Called with a number, it creates an array with that number of empty (null) elements.
Called with a non-number, it creates an array containing only that value.
Called with two or more values, it creates an array containing those values.
Called with an array literal, it creates an array from that literal.
Return Value
| Type | Description |
| Array | A new Array object. |
Array Tutorials:
More Examples
Example
Create an empty array and add values:
// Create a new Array
const cars = new Array();
// Add Values to the Set
cars.push("Saab");
cars.push("Volvo");
cars.push("BMW");
Try it Yourself »
Example
Create an array from a literal:
// Create a new Array
const cars = new Array(["Saab", "Volvo", "BMW"]);
Try it Yourself »
Example
Create an array with 10 empty (null) elements:
// Create an Array
const cars = new Array(10);
Try it Yourself »
Example
Create an array with one element:
// Create an Array
const cars = new Array("10");
Try it Yourself »
Example
Create an array without the new Array() method:
// Create an Array
const cars = ["Saab", "Volvo", "BMW"];
Try it Yourself »
Browser Support
new Array() is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |