Horje
How to Declare an Array in JavaScript ?

In JavaScript, Arrays can contain any type of data, numbers, strings, booleans, objects, etc. But typically all elements are of the same type.

Arrays can be single-dimensional with one level of elements, or multi-dimensional with nested arrays.

Following are the ways to declare an Array in JavaScript:

Approach 1: Using Array Literal

Array literals use square brackets to enclose comma-separated elements for initializing arrays in JavaScript.

Syntax:

const arrayName = [element1, element2, element3];

Example: This example shows the different types of array declaration.

JavaScript
// Number array
const numbers = [56, 74, 32, 45];

// String array
const courses = ["Javascript",
    "Python", "Java"];

// Empty array
const empty = [];

// Multidimensional array
const matrix = [
    [0, 1],
    [2, 3],
];
console.log(numbers);
console.log(courses);
console.log(empty);
console.log(matrix);

Output
[ 56, 74, 32, 45 ]
[ 'Javascript', 'Python', 'Java' ]
[]
[ [ 0, 1 ], [ 2, 3 ] ]

Approach 2: Using Array constructor

The Array constructor allows dynamically creating arrays and setting their initial state. It allows passing in elements as arguments to populate the array. If no arguments are passed, an empty array is created.

Syntax:

const arrayName = Array(element0, element1, ..., elementN);

Example: This example shows the declaration of an array using the array constructor.

JavaScript
// Number array
const numbers = Array(56, 74, 32, 45);

// String array
const courses = Array("Javascript",
    "Python", "Java");

// Empty array
const empty = Array();

// Multidimensional array
const matrix = Array([
    [0, 1],
    [2, 3],
]);

console.log(numbers);
console.log(courses);
console.log(empty);
console.log(matrix);

Output
[ 56, 74, 32, 45 ]
[ 'Javascript', 'Python', 'Java' ]
[]
[ [ [ 0, 1 ], [ 2, 3 ] ] ]

Approach 3: Using new keyword

The new Array() constructor initializes the array and returns a reference to the array object. It can be used to create arrays of different data types like numbers, strings, nested arrays etc.

Syntax:

const arrayName = new Array(element0, element1, ..., elementN);

Example: This example shows that we can create an array using new keyword also.

JavaScript
// Number array
const numbers = new Array(56, 74, 32, 45);

// String array
const courses = new Array("Javascript",
    "Python", "Java");

// Empty array
const empty = new Array();

// Multidimensional array
const matrix = new Array([
    [0, 1],
    [2, 3],
]);

console.log(numbers);
console.log(courses);
console.log(empty);
console.log(matrix);

Output
[ 56, 74, 32, 45 ]
[ 'Javascript', 'Python', 'Java' ]
[]
[ [ [ 0, 1 ], [ 2, 3 ] ] ]

Approach 4: Using Array.of

The Array.of method creates a new Array instance with a variable number of elements. It is especially useful to ensure that single numeric arguments are treated as elements rather than array lengths (which is a peculiarity of the Array constructor).

Example: This example demonstrates the use of Array.of to declare arrays:

JavaScript
// Number array
const numbers = Array.of(56, 74, 32, 45);
 
// String array
const courses = Array.of("Javascript", "Python", "Java");
 
// Empty array
const empty = Array.of();
 
// Multidimensional array
const matrix = Array.of([0, 1], [2, 3]);
 
console.log(numbers);
console.log(courses);
console.log(empty);
console.log(matrix);

Output
[ 56, 74, 32, 45 ]
[ 'Javascript', 'Python', 'Java' ]
[]
[ [ 0, 1 ], [ 2, 3 ] ]

Approach 5: Using Array.from

The Array.from method creates a new array instance 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: This example demonstrates the use of Array.from to declare arrays:

JavaScript
const stringArray = Array.from("JavaScript");
const setArray = Array.from(new Set([1, 2, 3, 4]));

function createArray() {
    return Array.from(arguments);
}
const argsArray = createArray(5, 10, 15, 20);

const mappedArray = Array.from([1, 2, 3, 4], x => x * 2);

console.log(stringArray);
console.log(setArray);
console.log(argsArray);
console.log(mappedArray);

Output
[
  'J', 'a', 'v', 'a',
  'S', 'c', 'r', 'i',
  'p', 't'
]
[ 1, 2, 3, 4 ]
[ 5, 10, 15, 20 ]
[ 2, 4, 6, 8 ]



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
What is Callback Hell in JavaScript ? What is Callback Hell in JavaScript ?
Pass by Value in JavaScript Pass by Value in JavaScript
JavaScript Program to Check Prime Number By Creating a Function JavaScript Program to Check Prime Number By Creating a Function
JavaScript Program to Print Prime Numbers from 1 to N JavaScript Program to Print Prime Numbers from 1 to N
JavaScript Program to Display Armstrong Number Between Two Intervals JavaScript Program to Display Armstrong Number Between Two Intervals

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14