Javascript Array() constructor

Introduction

The Array() constructor is used to create Array objects.

new Array(element0, element1[, ...[, elementN]])
new Array(arrayLength)

In the first form, a JavaScript array is initialized with the given elements.

When a single number typed argument is passed to the Array constructor and Javascript will use the constructor in the second form.

In the second form, JavaScript creates an array with its length property set to arrayLength.

Arrays can be created using the literal notation:

let languages = ['HTML', 'CSS'];

console.log(languages.length); // 2
console.log(languages[0]);     // "HTML"

The following code creates array with element count.

let languages = new Array(2);

console.log(languages.length); // 2
console.log(languages[0]);     // undefined

The following code creates an array with list of elements.

let languages = new Array('HTML', 'CSS');

console.log(languages.length); // 2
console.log(languages[0]);     // "HTML"



PreviousNext

Related