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:
new Array()
This constructor creates a new empty array.
Returns value of type Array
.
new Array(element1)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to include in the array. |
This constructor creates a new array with one element.
Returns value of type Array
.
new Array(element1, element2)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to include in the array. |
element2 | required | The second element to include in the array. |
This constructor creates a new array with two elements.
Returns value of type Array
.
new Array(element1, element2, /* …, */ elementN)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to include in the array. |
element2 | required | The second element to include in the array. |
elementN | optional | Additional elements to include in the array. |
This constructor creates a new array with one or more elements.
Returns value of type Array
.
new Array(arrayLength)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
arrayLength | required | The length of the array to create. |
This constructor creates a new array with a specified length.
Returns value of type Array
.
Array()
This constructor creates a new empty array.
Returns value of type Array
.
Array(element1)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to include in the array. |
This constructor creates a new array with one element.
Returns value of type Array
.
Array(element1, element2)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to include in the array. |
element2 | required | The second element to include in the array. |
This constructor creates a new array with two elements.
Returns value of type Array
.
Array(element1, element2, /* …, */ elementN)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to include in the array. |
element2 | required | The second element to include in the array. |
elementN | optional | Additional elements to include in the array. |
This constructor creates a new array with one or more elements.
Returns value of type Array
.
Array(arrayLength)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
arrayLength | required | The 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,
- We use the Array constructor with no arguments to create a new empty array.
- The result is stored in the variable
emptyArray
. - We log
emptyArray
to the console usingconsole.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,
- We use the Array constructor with one argument,
1
, to create a new array with one element. - The result is stored in the variable
singleElementArray
. - We log
singleElementArray
to the console usingconsole.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,
- We use the Array constructor with multiple arguments, 1, 2, and 3, to create a new array with three elements.
- The result is stored in the variable
multipleElementsArray
. - We log
multipleElementsArray
to the console usingconsole.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,
- We use the Array constructor with one argument,
5
, to create a new array with a length of 5. - The result is stored in the variable
arrayWithLength
. - We log
arrayWithLength
to the console usingconsole.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.