Creating Arrays
There are several ways to create an array. The first case is using the literal syntax. The second is calling the JavaScript Array object constructor.
let array1 = [1,2,3];
let array2 = new Array(1,2,3);
let array3 = new Array('1', '2', '3');
An odd quirk of the JavaScript language that can cause an unintentional error is the following:
// This is creating an array of length 1,
// not an array with an initial value of 1!
let array4 = new Array(1);
JavaScript also allows you to omit the new keyword in the Array constructor syntax.
// these are equivalent
let array1 = new Array(1,2,3);
let array2 = Array(1,2,3);
Last updated
Was this helpful?