Categories
JavaScript Answers

How to declare an array in JavaScript?

Spread the love

To declare an array in JavaScript, we use array literals.

For instance, we write

const x = [];
const y = [10];

const z = [];
z[2] = 12;

to declare array x which is an empty array.

We declare y which is an array with 10 in it.

And we declare array z as an empty array and then set 12 as the 3rd element in z.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *