Javascript Tutorial
Javascript Tutorial Introduction to Javascript JavaScript Code Editors & IDE JavaScript Hello World Example Javascript Syntax and Rules syntax_rules javascript_identifiers JavaScript Keywords & Reserved Words javascript_variables JavaScript Const JavaScript let vs var vs const Data Types in JavaScript JavaScript String Template Literals & String interpolation in JavaScript Tagged Templates in JavaScript String to Number in JavaScript Number Data Type in JavaScript NaN in JavaScript JavaScript Number Min & Max & Safe Values JavaScript EPSILON & Floating point precision Infinity in JavaScript JavaScript Bigint BigInt Vs Number in JavaScript Boolean Data Type in JavaScript Undefined in JavaScript Null in JavaScript Null vs Undefined in JavaScript JavaScript Operators Arithmetic Operators in JavaScript Unary plus & minus operators in JavaScript Increment & Decrement Operators in JavaScript Comparison or Relational operators in JavaScript Strict Equality (==) Loose Equality (===) in JavaScript Ternary Conditional Operator in JavaScript Logical Operators in JavaScript Bitwise Operators in JavaScript Assignment Operators in JavaScript Nullish Coalescing Operator in JavaScript Comma Operator in JavaScript Typeof JavaScript Operator Precedence in JavaScript JavaScript if, else & nested if statement Switch Statement in JavaScript While & Do While Loops in JavaScript For Loop in JavaScript Break statement in JavaScript Continue Statement in JavaScript Arrays in JavaScript Array Constructor in Javascript Sparse Array Vs Dense Array in JavaScript How to merge Arrays in JavaScript Array Methods in JavaScript Functions in JavaScript Function Parameters & Arguments in JavaScript JavaScript Default Parameters Pass by Value and Pass by Reference in Javascript Function Expression in Javascript Nested Functions in JavaScript Immediately-invoked Function Expressions (IIFE) JavaScript Callback Functions Arrow Functions in JavaScript Arguments Object In JavaScript Rest Parameters in JavaScript Objects in Javascript Create Objects in JavaScript JavaScript Object Properties Computed Property Names in JavaScript Object Literal in JavaScript Constructor Function & New Operator in JavaScript Delete Operator in JavaScript hasOwnProperty in JavaScript Using Getters and Setters in Javascript DefineProperty in JavaScript JavaScript Property Descriptors Enumerable, Writable & Configurable Object Destructuring in JavaScript Variable Scope in JavaScript Hoisting in JavaScript Lexical Scope & Closures in JavaScript This in JavaScript Global Object, Window & Globalthis in JavaScript Call function in Javascript Prototype In Javascript Prototype Inheritance in JavaScript Instanceof Operator in JavaScript Spread Operator in JavaScript

Array Constructor in Java Script

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.

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.

                            
new Array(element1, element2, element3,..., elementN)
 
                            
                        
                            
                        

The following example creates arrays of books with 4 elements.

                         
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.

                         
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.

                         
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)

                            
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

                            
arr= new Array(5)
console.log(arr)      //[ <5 empty items> ]
 
 
 
                            
                        
                            
                        

The above is the same as the following array literal syntax

                            
arr= [,,,,,]
console.log(arr)   //[ <5 empty items> ]
 
 
 
                            
                        
                            
                        

The number passed as a string will create only one element.

                            
 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

                            
let arr= new Array(-10)
console.log(arr)      //RangeError: Invalid array length
 
 
 
                            
                        
                            
                        

This is will create an array with two elements

                            
let arr= new Array(-10,-20)
console.log(arr)      //[ -10, -20 ]
 
 
 
                            
                        
                            
                        

Creates an empty array. i.e array with no elements & zero length

                            
let arr= new Array(0)
console.log(arr)  
 
 
 
                            
                        
                            
                        

The following creates an array with two elements.

        
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

                            
let arr= new Array("5")
let arrConsFn= arr.constructor
 
                            
                        
                            
                        

Since it is a constructor function, we can use it to create new arrays

                            
let arr= new Array("5")
let arrConsFn= arr.constructor
 
let arr1= new arrConsFn("1","2")
console.log(arr1)   //[ '1', '2' ]
 
console.log(arr)   //[ '5' ]
 
                            
                        
                            
                        

References

  1. Array