How to Declare an Array in JavaScript?
Last Updated :
22 Oct, 2024
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 Notation
The basic method to declare an array is using array litral notations. Use square bracket [] to declare an array and enclose comma-separated elements for initializing arrays.
Example: This example shows the different types of array declaration.
JavaScript
// Empty Array
const arr = [];
console.log(arr);
// Number Array
const numArr = [10, 20, 30, 40, 50];
console.log(numArr);
// String Array
const strArr = ["HTML", "CSS", "JS", "React"];
console.log(strArr);
// Multidimensional Array
const mat = [
[0, 1],
[2, 3],
];
console.log(mat);
Output[]
[ 10, 20, 30, 40, 50 ]
[ 'HTML', 'CSS', 'JS', 'React' ]
[ [ 0, 1 ], [ 2, 3 ] ]
Using Array() Constructor
Array constructor allows dynamically creating array and setting their initial state. It allows passing in elements as arguments to populate the array. If no argument is passed, an empty array is created.
Syntax
const arrayName = new Array(element0, element1, ..., elementN);
Example: Declaring an array using Array() constructor.
JavaScript
// Empty Array
const arr = new Array();
console.log(arr);
// Number Array
const numArr = new Array(10, 20, 30, 40, 50);
console.log(numArr);
// String Array
const strArr = new Array("HTML", "CSS", "JS", "React");
console.log(strArr);
// Multidimensional Array
const mat = new Array(Array(0, 1),Array(2, 3));
console.log(mat);
Output[]
[ 10, 20, 30, 40, 50 ]
[ 'HTML', 'CSS', 'JS', 'React' ]
[ [ 0, 1 ], [ 2, 3 ] ]
Using Array.of() Method
The Array.of() method creates a new Array with the specified elements. It is useful to ensure that single numeric argument is treated as elements rather than array lengths.
Example: Declare an array using Array.of() method.
JavaScript
// Empty Array
const arr = Array.of();
console.log(arr);
// Number Array
const numArr = Array.of(10, 20, 30, 40, 50);
console.log(numArr);
// String Array
const strArr = Array.of("HTML", "CSS", "JS", "React");
console.log(strArr);
// Multidimensional Array
const mat = Array.of(Array.of(0, 1), Array.of(2, 3));
console.log(mat);
Output[]
[ 10, 20, 30, 40, 50 ]
[ 'HTML', 'CSS', 'JS', 'React' ]
[ [ 0, 1 ], [ 2, 3 ] ]
Using Array.from() Method
The Array.from() method creates a new array from an array-like or iterable object. It allows for mapping functions to be applied to each element during the creation of the array.
Example 1: Converting a string to an array using Array.from() method.
JavaScript
let str = Array.from("Hello");
console.log(str);
Output[ 'H', 'e', 'l', 'l', 'o' ]
Similar Reads
How Dynamic Arrays Work in JavaScript ? A dynamic array, also known as a resizable array or a dynamic array list, is a data structure that allows its size to be changed dynamically during program execution. Unlike static arrays, which have a fixed size determined at the time of declaration, dynamic arrays can grow or shrink in size as nee
3 min read
How to Get the Size of an Array in JavaScript To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntaxconst a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that
2 min read
How to initialize an array in JavaScript ? 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 LiteralInitia
3 min read
How to Declare an Empty Array in TypeScript? TypeScript provides a robust type system that allows you to define and manage data structures more effectively than in plain JavaScript. One common task is declaring empty arrays with specific types, ensuring that your array holds only values of the intended type. These are the following ways to dec
2 min read
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 Convert Array to Set in JavaScript? The Set constructor is used to convert the array into set in JavaScript. It is useful to clean the array and remove duplicate elements. Converting Array to Set means transforming the array elements into a Set object.Using Set ConstructorThe Set constructor in JavaScript directly converts an array in
2 min read
How to Declare Two Dimensional Empty Array in JavaScript ? In this article, we are going to learn about Declare two-dimensional empty array by using JavaScript. A two-dimensional array is also known as a 2D array. It is a data structure in JavaScript that can hold values in rows and columns form. It is an array of arrays. Each element in a 2D array is acces
3 min read
How to Find the Length of an Array in JavaScript ? JavaScript provides us with several approaches to count the elements within an array. The length of an array lets the developer know whether an element is present in an array or not which helps to manipulate or iterate through each element of the array to perform some operation. Table of Content Usi
3 min read
How to Loop Through an Array in JavaScript? Here are the various ways to loop through an array in JavaScript1. Using for LoopThe for loop is one of the most used ways to iterate over an array. It gives you complete control over the loop, including access to the array index, which can be useful when you need to modify elements or perform other
4 min read
JavaScript - Insert Element in an array In JavaScript elements can be inserted at the beginning, end, and at any specific index. JS provides several methods to perform the operations.At the Beginning This operation inserts an element at the start of the array. The unshift() method is commonly used, which mutates the original array and ret
2 min read