How to initialize an array in JavaScript ?
Last Updated :
22 Oct, 2024
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.

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);
OutputEmpty 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);
OutputArray 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);
OutputEmpty 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);
OutputArray 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);
Difference Between Array() Constructor and Array Literal []
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 |
---|
Similar Reads
How to initialize a boolean array in JavaScript ? A Boolean array is a collection of variables that can hold only two values: true or false. In JavaScript, Boolean values represent the simplest forms of truthiness and are essential for controlling flow in your programs, such as in conditional statements and loops.Why Use Boolean Arrays?Boolean arra
3 min read
How to Declare and Initialize an Array in Java? An array in Java is a linear data structure that is used to store multiple values of the same data type. In an array, each element has a unique index value, which makes it easy to access individual elements. We first need to declare the size of an array because the size of the array is fixed in Java
5 min read
How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one
2 min read
How to clone an array in JavaScript ? In JavaScript, cloning an array means creating a new array with the same elements as the original array without modifying the original array.Here are some common use cases for cloning an array:Table of ContentUsing the Array.slice() MethodUsing the spread OperatorUsing the Array.from() MethodUsing t
6 min read
How to Declare an Array in JavaScript? Array in JavaScript are used to store multiple values in a single variable. It can contain any type of data like - numbers, strings, booleans, objects, etc. There are varous ways to declare arrays in JavaScript, but the simplest and common is Array Litral Notations. Using Array Literal NotationThe b
3 min read
How to Store an Object Inside an Array in JavaScript ? Storing an object inside an array in JavScript involves placing the object as an element within the array. The array becomes a collection of objects, allowing for convenient organization and manipulation of multiple data structures in a single container. Following are the approaches through which it
3 min read