To declare an array in JavaScript, you can use the array literal notation or the Array constructor.
In this example,
numbers
using the array literal notation []
and initialize it with some values.let numbers = [1, 2, 3, 4, 5];
console.log('Array elements:', numbers);
Array elements: [ 1, 2, 3, 4, 5 ]
In this example,
fruits
using the Array constructor new Array()
.let fruits = new Array();
fruits[0] = 'Apple';
fruits[1] = 'Banana';
fruits[2] = 'Orange';
console.log('Array elements:', fruits);
Array elements: [ 'Apple', 'Banana', 'Orange' ]
In this tutorial, we learned How to Declare an Array in JavaScript language with well detailed examples.