Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is Javascript array constructor?

Muhammad Ashir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

Prototype

//literal constructor
[element1, element2, ..., elementn]

//construct from elements
new Array(element1, element2, ..., elementN)

//construct from arrayLength
new Array(arrayLength)

Parameters

  • elementN: An array is initialized with the given elements (elementN), except in the case where only one element is provided and that one element is also a number. However, if an array is constructed using the [] operator, then the previous rule does not apply.

  • arrayLength: If the argument is only one element that is also a number between 0 and 2^32 - 1 inclusive, an array object is created with the length property set to arrayLength.

Code

//using bracket operator
const array1 = [1, 4, 7, 0]
//using elements
const array2 = new Array(1, 4, 7, 0)
//using arrayLength
const array3 = new Array(10)
console.log(array1)
console.log(array2)
console.log(array3)

The above code shows the examples of array constructors used in each of the three contexts. Note that array3 is constructed with 1010 empty spaces instead of undefined objects.

RELATED TAGS

javascript

CONTRIBUTOR

Muhammad Ashir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring