Creating Arrays using Javascript


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,

console.log(myArr);

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.

Sai Subramanyam
Sai Subramanyam

Passionate, Curious and Enthusiastic.

Updated on: 15-Jun-2020

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements