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 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 Empty an Array in JavaScript? To empty an array in JavaScript, we can use the array literal. We can directly assign an empty array literal to the variable, it will automatically remove all the elements and make the array empty.1. Empty Array using Array LiteralWe can empty the array using the empty array literal syntax. Syntaxar
2 min read
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 declare namespace in JavaScript ? The coding standard of assigning scope to identifiers (names of types, functions, variables, and so on) to avoid conflicts between them is known as a namespace. It is a framework for a collection of identifiers, functions, and methods. It gives its contents a sense of direction so that they are easi
3 min read
Convert an Array to JSON in JavaScript Given a JavaScript Array and the task is to convert an array to JSON Object. Below are the approaches to convert an array to JSON using JsvaScript: Table of ContentJSON.stringify() methodObject.assign() methodJSON.stringify() methodThe use of JSON is to exchange data to/from a web server. While send
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