Skip to content

JavaScript Array constructor property | create Array objects

  • by

JavaScript Array() constructor is used to create Array objects. The constructor property returns the function that created the Array prototype.

array.constructor 

In JavaScript, the array.constructor property returns a reference to the constructor function that created the array. It allows you to access the constructor function that was used to create the array object.

const fruits = ['apple', 'banana', 'orange'];
console.log(fruits.constructor); // Output: [Function: Array]

Use JavaScript Array constructor property

Simple example code constructor property.

<!DOCTYPE html>
<html>
<body>

  <script>

   let languages = ["JavaScript", "Java", "Python"];

   let res = languages.constructor;
   console.log(res)

 </script>

</body>
</html> 

Output:

JavaScript Array constructor property

Array constructor with a single parameter

It’s important to note that when using the Array constructor with a single numeric argument, it specifies the length of the array rather than the actual values of the elements.

let fruits = new Array(2);

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

Array constructor with multiple parameters

When using the Array constructor with multiple parameters, each parameter is treated as an individual element of the array.

let fruits = new Array('Apple', 'Banana');

console.log(fruits.length); // 2
console.log(fruits[0]);     // "Apple"

You can pass any valid JavaScript expression as a parameter to the Array constructor, not just simple values. For instance:

const myArray = new Array(1, 2 + 3, 'Hello', true);
console.log(myArray); // Output: [1, 5, 'Hello', true]

Do comment if you have any doubts or suggestions on this JS Array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *