How to Create a New Array in JavaScript?

Estimated read time 1 min read

In JavaScript, you can create an array using the Array constructor or using square brackets [].

Using the Array constructor:

var arrayName = new Array(element0, element1, element2, ..., elementX);

Using square brackets:

var arrayName = [element0, element1, element2, ..., elementX];

Here, element0, element1, element2, …, elementX are the elements you want to include in the array.

For example:

var fruits = new Array("apple", "banana", "cherry");

or

var fruits = ["apple", "banana", "cherry"];

Both of these arrays will have the same elements, and you can access them using the index of the elements, for example fruits[0] would give you the first element, which is "apple".

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply