JavaScript Array()
Syntax & Examples


Array constructor

The Array constructor in JavaScript creates a new Array object. It can be used with or without the `new` keyword.


Syntax of Array

There are 10 variations for the syntax of Array() constructor. They are:

1.
new Array()

This constructor creates a new empty array.

Returns value of type Array.

2.
new Array(element1)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to include in the array.

This constructor creates a new array with one element.

Returns value of type Array.

3.
new Array(element1, element2)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to include in the array.
element2requiredThe second element to include in the array.

This constructor creates a new array with two elements.

Returns value of type Array.

4.
new Array(element1, element2, /* …, */ elementN)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to include in the array.
element2requiredThe second element to include in the array.
elementNoptionalAdditional elements to include in the array.

This constructor creates a new array with one or more elements.

Returns value of type Array.

5.
new Array(arrayLength)

Parameters

ParameterOptional/RequiredDescription
arrayLengthrequiredThe length of the array to create.

This constructor creates a new array with a specified length.

Returns value of type Array.

6.
Array()

This constructor creates a new empty array.

Returns value of type Array.

7.
Array(element1)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to include in the array.

This constructor creates a new array with one element.

Returns value of type Array.

8.
Array(element1, element2)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to include in the array.
element2requiredThe second element to include in the array.

This constructor creates a new array with two elements.

Returns value of type Array.

9.
Array(element1, element2, /* …, */ elementN)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to include in the array.
element2requiredThe second element to include in the array.
elementNoptionalAdditional elements to include in the array.

This constructor creates a new array with one or more elements.

Returns value of type Array.

10.
Array(arrayLength)

Parameters

ParameterOptional/RequiredDescription
arrayLengthrequiredThe length of the array to create.

This constructor creates a new array with a specified length.

Returns value of type Array.



✐ Examples

1 Using Array constructor to create an empty array

In JavaScript, we can use the Array constructor to create a new empty array.

For example,

  1. We use the Array constructor with no arguments to create a new empty array.
  2. The result is stored in the variable emptyArray.
  3. We log emptyArray to the console using console.log() method to see the created array.

JavaScript Program

const emptyArray = new Array();
console.log(emptyArray);

Output

[]

2 Using Array constructor to create an array with one element

We can use the Array constructor to create a new array with one element.

For example,

  1. We use the Array constructor with one argument, 1, to create a new array with one element.
  2. The result is stored in the variable singleElementArray.
  3. We log singleElementArray to the console using console.log() method to see the created array.

JavaScript Program

const singleElementArray = new Array(1);
console.log(singleElementArray);

Output

[1]

3 Using Array constructor to create an array with multiple elements

We can use the Array constructor to create a new array with multiple elements.

For example,

  1. We use the Array constructor with multiple arguments, 1, 2, and 3, to create a new array with three elements.
  2. The result is stored in the variable multipleElementsArray.
  3. We log multipleElementsArray to the console using console.log() method to see the created array.

JavaScript Program

const multipleElementsArray = new Array(1, 2, 3);
console.log(multipleElementsArray);

Output

[1, 2, 3]

4 Using Array constructor to create an array with a specified length

We can use the Array constructor to create a new array with a specified length.

For example,

  1. We use the Array constructor with one argument, 5, to create a new array with a length of 5.
  2. The result is stored in the variable arrayWithLength.
  3. We log arrayWithLength to the console using console.log() method to see the created array.

JavaScript Program

const arrayWithLength = new Array(5);
console.log(arrayWithLength);

Output

[, , , , ]

Summary

In this JavaScript tutorial, we learned about Array constructor of Array: the syntax and few working examples with output and detailed explanation for each example.