Open In App

How to initialize an array in JavaScript ?

Last Updated : 22 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Initializing an array in JavaScript involves creating a variable and assigning it an array literal. The array items are enclosed in square bracket with comma-separated elements. These elements can be of any data type and can be omitted for an empty array.

Array initialising

Initialize an Array using Array Literal

Initializing an array using the array literal involves directly assigning square brackets [] to a variable. It creates an empty array or populates it with elements enclosed within the brackets.

Example: Create an array using array literal.

JavaScript
const arr1 = [];

console.log("Empty Array: ", arr1);

const arr2 = [10, 20, 30, 40, 50];

console.log("Array with Items: ", arr2);

Output
Empty Array:  []
Array with Items:  [ 10, 20, 30, 40, 50 ]

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

JavaScript
const arr = [
10,
20,
30,
40,
50
];

console.log("Array Elements: ", arr);

Output
Array Elements:  [ 10, 20, 30, 40, 50 ]


Other options to initialize an array in JavaScript are listed below:

Using Array() Constructor

Initializing an array using Array constructor involves invoking new Array() and optionally passing initial elements as arguments. Alternatively, specifying the desired length as a single argument creates an array with undefined elements.

Example: Creating and initializing an array using array() constructor. It creates an array as an object.

JavaScript
// Create an empty array
const arr1 = new Array();
console.log("Empty Array", arr1);

// Create and Initialize an array
const arr2 = new Array(10, 20, 30, 40, 50);
console.log("Array Elements: ", arr2);

// Create an Array of Given size
const arr3 = new Array(5);
console.log("Array of size 5: ", arr3);

Output
Empty Array []
Array Elements:  [ 10, 20, 30, 40, 50 ]
Array of size 5:  [ <5 empty items> ]

Using Array.of() Method

The Array.of() method in JavaScript creates a new array instance with a variable number of elements. This method ensures the exact elements are added to the array, avoiding unexpected behaviors with other array creation methods like Array() constructor.

Example: Initializing an array using array.of() method.

JavaScript
let arr = Array.of(1, 2, 3, 4);
console.log("Array Elements: ", arr);

Output
Array Elements:  [ 1, 2, 3, 4 ]

Using Array.from() Method

The Array.from() method creates an array from iterable objects. It takes two arguments: an object with a length property and a map function. The map function can be used to initialize elements.

Example: Creating an array of length 5 using Array.from() with values incrementing from 1 to 5.

JavaScript
const arr = Array.from({ length: 5 }, (_, i) => i + 1);
console.log(arr);

Output
[ 1, 2, 3, 4, 5 ]

Difference Between Array() Constructor and Array Literal []

FeatureArray() ConstructorArray Literal ([])
Type of CreationBuilt-in constructor function for creating arrays.Shorthand notation for creating arrays.
Call with new KeywordCan be called with or without the new keyword.Not applicable, as it is a shorthand notation, not a function.
Object CreationWhen called with new, it creates a new array object.Not applicable, as it doesn't involve explicit object creation.
Return TypeReturns an array object.Returns a newly created array.
ArgumentsCan take arguments to specify initial elements of the array.Can be used to create empty arrays or initialize with elements.
Examplejavascript 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

Next Article

Similar Reads