An array constructor is a constructor function that allows us to create a new JavaScript Array. Every Javascript array also has a constructor property, which points to the constructor function.
Table of Contents
How to use Array Constructor
JavaScript has a global Array constructor function. We use the new keyword to invoke it. It creates a new array using the arguments. Except when it has only one argument which is numeric, then it will create an array with a length equal to the provided argument.
The basic syntax is as follows.
1 2 3 | new Array(element1, element2, element3,..., elementN) |
The following example creates arrays of books with 4 elements.
1 2 3 | const books = new Array("Ulysses", "Don Quixote", "War and Peace", "Moby Dick"); |
The “Ulysses” is stored at index 0, “Don Quixote” at index 1, etc.
1 2 3 4 5 6 7 8 | const books = new Array("Ulysses", "Don Quixote", "War and Peace", "Moby Dick"); console.log(books[0]) //Ulysses console.log(books[1]) //Don Quixote console.log(books[2]) //War and Peace console.log(books[3]) //Moby Dick |
Note that calling the Array function without new also returns the array without any side effects. However, you should not invoke a constructor function without a new keyword.
1 2 3 4 | books = Array("Ulysses", "Don Quixote", "War and Peace", "Moby Dick"); console.log(books) |
Array Constructor with one numeric argument
The following code creates an array with two elements ( 5 & 10)
1 2 3 4 | arr=new Array(5,10) console.log(arr) //[ 5, 10 ] |
But if you pass only one numeric argument, then it will create an empty array with a length equal to the argument. For example, new Array(5)
will create an array with length 5 but without any elements. Such an array is known as Sparse Array
1 2 3 4 | arr= new Array(5) console.log(arr) //[ <5 empty items> ] |
The above is the same as the following array literal syntax
1 2 3 4 | arr= [,,,,,] console.log(arr) //[ <5 empty items> ] |
The number passed as a string will create only one element.
1 2 3 4 | arr= new Array("5") console.log(arr) //[ '5' ] |
If you pass a -ve number, the Javascript will throw an error. In fact, if we pass a single numeric argument whose value is not between 0 and 232 – 1 (inclusive) an error will be thrown
1 2 3 4 | let arr= new Array(-10) console.log(arr) //RangeError: Invalid array length |
This is will create an array with two elements
1 2 3 4 | let arr= new Array(-10,-20) console.log(arr) //[ -10, -20 ] |
Creates an empty array. i.e array with no elements & zero length
1 2 3 4 | let arr= new Array(0) console.log(arr) |
The following creates an array with two elements.
1 2 3 4 | let arr= new Array(0,0) console.log(arr) //[ 0, 0 ] |
Constructor Property
Every Array has a constructor property, which returns the constructor function that created the Array. It actually returns the Array Constructor function, because all arrays are created using that constructor function.
arr
is the Array with one element. We can access its constructor property using arr.constructor
1 2 3 4 | let arr= new Array("5") let arrConsFn= arr.constructor |
Since it is a constructor function, we can use it to create new arrays
1 2 3 4 5 6 7 8 9 | let arr= new Array("5") let arrConsFn= arr.constructor let arr1= new arrConsFn("1","2") console.log(arr1) //[ '1', '2' ] console.log(arr) //[ '5' ] |