How to useArray constructor in Javascript
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); |
[ 56, 74, 32, 45 ] [ 'Javascript', 'Python', 'Java' ] [] [ [ [ 0, 1 ], [ 2, 3 ] ] ]
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.