JavaScript Array Constructor Property Array.constructor property

This JavaScript tutorial covers the JavaScript array constructor property. The constructor property points to the function that created the object in question: in the case of arrays (a native JavaScript data type), the constructor is the array constructor, called with 'new Array()'.

What is the JavaScript Array Constructor Property?

The constructor property is available to all JavaScript objects; it points to the ultimate function called to create the object type, without concerns for the way in which the object was created. For example, a JavaScript array can be created with the new Array() constructor, the square-bracketed literal notation, or indirectly returned from a method like String.split(). Either way, the array generated has the same constructor.

var arlene = new Array();
alert( arlene.constructor.toString() );

The script above returns a string representation of the object constructor (in this case an array). The output varies from one JavaScript interpreter to another (depends on your browser's rendering engine); here is a sample: JavaScript array constructor

Using Constructors to Check Object Data Types

The constructor property can also be used to check an object's data type (much like the "typeof()" operator). Below, we declare both an array and a string, and check if their constructor is the array constructor: in other words, whether or not the array constructor was used to create the object.

var arlene = new Array();
alert( arlene.constructor === Array );

Executing this script confirms (true) or denies (false) that our object is an array: JavaScript array constructor property

The JavaScript code below would return false, because our string's constructor is not the Array constructor (it is the String constructor).

var carter = new String();
alert( carter.constructor === Array );

Constructor for Custom JavaScript Objects

As illustration, here is a custom JavaScript object; a custom function serves as constructor (it assigns properties and methods to any new instance of the given object, a "ScriptingLanguage" in our case).

function ScriptingLanguage(a,b) { = a;
    this.syntaxType = b;

var js = new ScriptingLanguage("JavaScript","C");
alert( js.constructor.toString() );

The last line of our script generates the following, a string representation of our "ScriptingLanguage" constructor function: JavaScript object constructor

Test the Array Constructor Property

Interactively test the array constructor property by editing the JavaScript code below and clicking the Test Array Constructor Property button.

Browser support for JavaScript array constructor property
Internet Explorer supports the JavaScript array constructor propertyFirefox supports the JavaScript array constructor propertySafari supports the JavaScript array constructor propertyOpera supports the JavaScript array constructor property