• JavaScript Video Tutorials

JavaScript - Array constructor Property



In JavaScript, the Array constructor property is used to return the constructor function for the array. For JavaScript arrays the constructor property returns: function Array() { [native code] }.

The Array constructor's return value is a reference value to the function, not the name of the function.

Syntax

Following is the syntax of JavaScript Array constructor −

array.constructor

Here, array is an array.

Return value

It returns the constructor function for the specified array.

Examples

Example 1

In the following example, we are using the JavaScript Array constructor property to return the constructor function for the "animals" array −

<html>
<body>
   <script>
      let animals = ["lion", "cheetah", "tiger", "elephant"];
      let result = animals.constructor;
      document.write(result);
   </script>
</body>
</html>

Output

If we execute the above program, it returns result as follows:

function Array() { [native code] }

Example 2

Note − Array() can be called with or without new. Both create a new Array instance.

In this example, we are calling the Array() with "new" keyword. Then we are using the Array constructor property to return the constructor function for the array −

<html>
<body>
   <script>
      let animals = new Array ("lion", "cheetah", "tiger", "elephant");
      let result = animals.constructor;
      document.write(result);
   </script>
</body>
</html>

Output

As the Array() with new keyword created a new Array instance, the Array constructor property returns result as follows:

function Array() { [native code] }

Example 3

Here, we are calling the Array() without "new" keyword. Then we are using the Array constructor property on it −

<html>
<body>
   <script>
      let animals = Array ("lion", "cheetah", "tiger", "elephant");
      let result = animals.constructor;
      document.write(result);
   </script>
</body>
</html>

Output

As the Array() without new keyword also creates a new Array instance, the Array constructor property returns result as follows:

function Array() { [native code] }
Advertisements