JavaScript new Array()
Examples
const cars = new Array("Saab", "Volvo", "BMW");
const b = new Array(3);
const c = new Array("3");
const d = new Array("Saab", "Volvo", "BMW");
More Examples Below !
Description
The new Array() constructor creates a new Array object.
Syntax
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
Array Tutorials:
More Examples
Example
Create an empty array and add values:
const cars = new Array();
// Add Values to the Set
cars.push("Saab");
cars.push("Volvo");
cars.push("BMW");
Example
Create an array from a literal:
const cars = new Array(["Saab", "Volvo", "BMW"]);
Example
Create an array with 10 empty (null) elements:
const cars = new Array(10);
Example
Create an array with one element:
const cars = new Array("10");
Example
Create an array without the new Array() method:
const cars = ["Saab", "Volvo", "BMW"];
Browser Support
new Array() is an ECMAScript1 (JavaScript 1997) feature.
It is supported in all browsers:
| Chrome | Edge | Firefox | Safari | Opera |