Creating Arrays using Javascript

Web DevelopmentFront End TechnologyJavascript

There are many ways to create an Array in JavaScript. We'll look at how to create an empty array first using 2 methods.

let myArr = [];
let myArr = new Array();

Both of the above lines create an empty array. The JavaScript community always prefers the first method as it is easier to read, type and performs the same task as the second one. You can also populate the array when you're creating it using either of the following 2 notations −

let myArr = ["Mon", "Tue", "Wed", "Thu", "Fri"];
let myArr = new Array("Mon", "Tue", "Wed", "Thu", "Fri");

You can print it on the console using a console.log statement. For example,


This will give the output.

["Mon", "Tue", "Wed", "Thu", "Fri"]

Though not used much, you can also create an array by specifying its length as follows −

let myArr = new Array(5);

This will create an array of size 5.

Published on 26-Jun-2018 16:52:54