Array object (Object/core)

An object of the class "Array".

Availability:

ECMAScript edition - 2
JavaScript - 1.1
JScript - 3.0
Internet Explorer - 4.0
Netscape - 3.0
Netscape Enterprise Server - 2.0
Opera - 3.0
JavaScript syntax:-myArray = Array
-myArray = myVBArray.toArray()
-myArray = new Array()
-myArray = new Array(aLength)
-myArray = new Array(anItem1, anItem2, anItem3, ...)
Argument list:aLengthAn optional initial length to set the array to.
anItemNA variable number of initial elements to insert into the array.
Object properties:constructor, index, input, length, prototype
Object methods:concat(), join(), pop(), push(), reverse(), shift(), slice(), sort(), splice(), toLocaleString(), toSource(), toString(), unshift(), valueOf()

An array is basically an indexed collection of references to other objects or values.

In JavaScript version 1.0, arrays were simple objects and had limited functionality, scarcely enough really to be called arrays. Some commentators argue that the functionality was so limited that they should be flagged as available from version 1.1 of JavaScript only. They were usually simulated by creating an instance of the Object object and using its named properties as if the object was an array.

Much additional functionality was added for JavaScript version 1.1. JavaScript version 1.0 lacked the constructors and arrays had no special methods available. The ECMA standard enhances the functionality and Netscape 4 provides additional functionality.

An instance of the class "Array" is created by using the new operator on the Array() constructor. At JavaScript version 1.2, Arrays can be created with an Array literal as well. The new object adopts the behavior of the built-in prototype object through the prototype-inheritance mechanisms.

All properties and methods of the prototype are available as if they were part of the instance.

Note that the index and input properties are available only for Arrays that are produced as the result of a RegExp match. They are not generally available in Arrays or Collections.

An array is a collection of properties owned by an object and which can be accessed by name or by index position in the array. Because they are collected together and accessible as a set, they may be sorted into order of the array.

Array objects give special treatment to property names, which are numeric values. These are used as an index value and will affect the value of the length property. The length supported depends on the platform, but is usually based on a 32 bit integer being used for addressing. That limits the range to 4 Billion array elements.

Array objects implement the Put() internal function slightly differently to non-array based objects.

The prototype for the Array prototype object is the Object prototype object.

In C language, an array is referred to as an aggregate type since it is made from a collection or aggregate of individual members.

Warnings:

Example code:

   <SCRIPT>// Array object demonstration

   var weekly_summary = new Array(7);

   weekly_summary[1] = 10;

   weekly_summary[2] = 25;

   var day_names = new Array("Su","Mo","Tu","We","Th","Fr","Sa");

   for(var i=0; i<7; i++)

   {

      document.write("Summary for day (");

      document.write(day_names[i]);

      document.write(") = ");

      document.write(weekly_summary[i]);

      document.write("<BR>");

   }

   </SCRIPT>

See also:Aggregate type, Array index delimiter ([ ]), Array literal, Array(), Array(), Array.Class, Array.length, Array.prototype, Collection object, JavaArray object, JellyScript, Native object, Object object, String.split(), unwatch(), VBArray.toArray(), watch()

PropertyJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
constructor1.1 1.13.0 3.03.0 3.04.0 4.0 n/a n/a2 2 n/a n/a n/a-
index1.2 1.25.5 5.54.0 4.05.5 5.5 n/a n/a n/a n/a n/a n/a-
input1.2 1.25.5 5.54.0 4.05.5 5.5 n/a n/a n/a n/a n/a n/a-
length1.0 1.03.0 3.02.0 2.04.0 4.0 n/a n/a n/a n/a n/a n/aReadOnly
prototype1.1 1.13.0 3.03.0 3.04.0 4.0 n/a n/a2 2 n/a n/a n/aReadOnly, DontDelete, DontEnum.

MethodJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
concat()1.2 1.23.0 3.04.0 4.04.0 4.0 n/a3.0 3.03 3 n/a n/a n/aWarning
join()1.1 1.13.0 3.03.0 3.04.0 4.03.0 3.02.0 2.02 2 n/a n/a n/a-
pop()1.2 1.25.5 5.54.0 4.05.5 5.5 n/a3.0 3.03 3 n/a n/a n/a-
push()1.2 1.25.5 5.54.0 4.05.5 5.5 n/a3.0 3.03 3 n/a n/a n/a-
reverse()1.1 1.13.0 3.03.0 3.04.0 4.03.0 3.02.0 2.02 2 n/a n/a n/a-
shift()1.2 1.25.5 5.54.0 4.05.5 5.5 n/a3.0 3.03 3 n/a n/a n/a-
slice()1.2 1.23.0 3.04.0 4.04.0 4.0 n/a3.0 3.03 3 n/a n/a n/aWarning
sort()1.1 1.13.0 3.03.0 3.04.0 4.03.0 3.02.0 2.02 2 n/a n/a n/aWarning
splice()1.2 1.25.5 5.54.0 4.05.5 5.5 n/a3.0 3.03 3 n/a n/a n/aWarning
toLocaleString()1.5 1.55.5 5.56.0 6.05.5 5.5 n/a n/a3 3 n/a n/a n/aWarning
toSource()1.3 1.33.0 3.04.06 4.064.0 4.0 n/a n/a n/a n/a n/a n/a-
toString()1.1 1.13.0 3.03.0 3.04.0 4.03.0 3.02.0 2.02 2 n/a n/a n/aWarning
unshift()1.2 1.25.5 5.54.0 4.05.5 5.5 n/a3.0 3.03 3 n/a n/a n/a-
valueOf()1.1 1.13.0 3.03.0 3.04.0 4.0 n/a n/a n/a n/a n/a n/a-

Cross-references:

ECMA 262 edition 2 - section - 8.6.2.2

ECMA 262 edition 2 - section - 15.4

ECMA 262 edition 3 - section - 8.6.2.2

ECMA 262 edition 3 - section - 15.4

Wrox Instant JavaScript - page - 15