How to useArray Literal in Javascript
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 ] ]
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.
Contact Us