Open In App

How to initialize an array in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

We need to initialize the arrays before using them up and once the arrays are initialized various other operations can be performed on them.

Array initialising

To initialize an array in JavaScript we can use methods given below:

Using an array as literal

It is easy to initialize arrays as literals as they can be directly initialized using commas and enclosed in square brackets. 

Example: Below is an example of array as literal.

Javascript




const sports = ["cricket", "football",
                "competitive-programming"];
console.log('sports=', sports);
 
const myArray = [];
console.log('myArray=', myArray);
 
const score = [420, 10, 1, 12, 102];
console.log('score=', score);


Output

sports= [ 'cricket', 'football', 'competitive-programming' ]
myArray= []
score= [ 420, 10, 1, 12, 102 ]

Example 2: The line breaks and new lines do not impact arrays, they store in their normal way.

Javascript




const sports = ["cricket",
            "football",
            "competitive-programming"];
console.log('sports=', sports);
 
const myArray = [];
console.log('myArray=', myArray);
 
const score = [420,10,1,
               12,102];
console.log('score=', score);


Output

sports= [ 'cricket', 'football', 'competitive-programming' ]
myArray= []
score= [ 420, 10, 1, 12, 102 ]

Using an array as an object/Array() constructor

Initializing an array with Array constructor syntax is done by using the new keyword. The similar array which was described earlier can be also declared as shown below.

Example: Below is an example of array as an object.

Javascript




const sports = new Array("cricket", "football", "competitive-programming");
console.log('sports=', sports);
 
const myArray = new Array();
console.log('myArray=', myArray);
 
const points = new Array();
console.log('points=', points);
 
const score = new Array(140, 200, 21, 53, 245, 20);
console.log('score=', score);


Output

sports= [ 'cricket', 'football', 'competitive-programming' ]
myArray= []
points= []
score= [ 140, 200, 21, 53, 245, 20 ]

The Difference Between Array() and []

In JavaScript, Array() and [] are two ways to create arrays, but there is a subtle difference between them.

Feature Array() Constructor Array Literal ([])
Type of Creation Built-in constructor function for creating arrays. Shorthand notation for creating arrays.
Call with new Keyword Can be called with or without the new keyword. Not applicable, as it is a shorthand notation, not a function.
Object Creation When called with new, it creates a new array object. Not applicable, as it doesn’t involve explicit object creation.
Return Type Returns an array object. Returns a newly created array.
Arguments Can take arguments to specify initial elements of the array. Can be used to create empty arrays or initialize with elements.
Example javascript const array1 = new Array(1, 2, 3);// Using constructor with new
const array2 = Array(4, 5, 6); // Using constructor without new
javascript const array3 = [7, 8, 9]; // Creating an array using literal notation const emptyArray = []; // Creating an empty array

When using Array() with the new keyword, it creates a new array object. When using [] (array literal notation), it also creates a new array object. However, if Array() is called without new and without arguments, it returns an empty array, while [] always creates a new empty array.

Example:

const newArray = Array(); // Returns an empty array
const literalArray = [];  // Creates a new empty array



Last Updated : 05 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads