Coderslang

Search IconIcon to open search

Create an Array in JavaScript [Tech Interview Quiz]

Last updated Jan 9, 2023

There are multiple ways to create arrays in JavaScript.

Consider the following code snippet and find the line that will trigger the error.

Code snippet as a picture

1
2
3
let a1 = [];
let a2 = Array();
let a3 = new Array();

What’s the correct way to create an array in JS?

Explanation

The first array, a1 is declared using an empty array literal. There are no issues with it. In fact, it’s one of the most common ways of declaring an array in JS.

The second one, a2 is created by calling Array as a function. It’s fine and it creates an empty array as well.

The third array, a3 is created using an explicit call to the Array constructor as we use the new keyword there.

Answer

There’s no error. All three array have been created correctly.

🔥 START LEARNING 🔥