ArrayNN 3   IE J2   ECMA 1

An array is an ordered collection of one or more pieces of data. JavaScript array entries may be of any data type, including different data types in the same array. Each entry in an array has an index assigned to it. The default behavior is for the index to be a zero-based integer (the first entry has an index of zero). An index value may also be a string. Accessing an entry in an array requires the name of the array and the index in square brackets:

cars[0]
cars["Ford"]

The number of entries in a JavaScript array (its length) can vary over time. To add a new entry to an array, assign the value to the next higher array index value:

cars[cars.length] = "Bentley"

Internet Explorer first recognized the Array object in Version 2 of the Jscript.dll for Internet Explorer 3.

 
Creating an Array
var myArray = new Array()
var myArray = new Array(sizeInteger)
var myArray = new Array(element0, element1, ..., elementN)
lengthNN 3   IE J2   ECMA 1
 Read/Write
 

A count of the number of entries stored in the array. If the constructor function used to create the array specified a preliminary length, the length property reflects that amount, even if data does not occupy every slot.

 
Example
for (var i = 0; i < myArray.length; i++) {
    ...
}
 
Value
Integer.
prototypeNN 3   IE J2   ECMA 1
 Read/Write
 

A property of the static Array object. Use the prototype property to assign new properties and methods to future instances of arrays created in the current document. For example, the following function creates a return-delimited list of elements in an array in reverse order:

function formatAsList() {
    var output = ""
    for (var i = this.length - 1; i >= 0; i--) {
        output += this[i] + "\n"
    }
    alert(output)
}

To give an array that power, assign this function reference to a prototype property whose name you want to use as the method to invoke this function:

Array.prototype.showReverseList = formatAsList

If a script creates an array at this point:

var stooges = new Array("Moe", "Larry", "Curly", "Shemp")

the new array has the showReverseList() method available to it. To invoke the method, the call is:

stooges.showReverseList()

You can add properties the same way. These allow you to attach information about the array (its creation time, for example) without disturbing the ordered sequence of array data. When a new document loads into the window or frame, the static Array object starts fresh again.

 
Example
Array.prototype.created = ""
 
Value
Any data, including function references.
concat()NN 4   IE J3   ECMA n/a
concat(array2) 
 

Returns an array that combines the current array object with a second array object specified as the method parameter:

var combinedArray = myArray1.concat(myArray2)

Neither of the original arrays is altered in the process.

 
Returned Value
An Array object.
 
Parameters
array2 Any JavaScript array.
join()NN 3   IE J2   ECMA 1
join("delimiterString") 
 

Returns a string consisting of a list of items (as strings) contained by an array. The delimiter character(s) between items is set by the parameter to the method.

 
Returned Value
String.
 
Parameters
delimiterString Any string of characters. Nonalphanumeric characters must use URL-encoded equivalents (0 for carriage return).
pop()NN 4   IE n/a   ECMA n/a

Returns the value of the last item in an array and removes it from the array. The length of the array decreases by one.

 
Returned Value
Any JavaScript value.
 
Parameters
None.
push()NN 4   IE n/a   ECMA n/a
push(value) 
 

Appends an item to the end of an array. The length of the array increases by one.

 
Returned Value
The value pushed into the array.
 
Parameters
value Any JavaScript value.
reverse()NN 3   IE J2   ECMA 1

Reverses the order of items in the array and returns a copy of the array in the new order. The original order of the array is changed after this method executes.

 
Returned Value
An Array object.
 
Parameters
None.
shift()NN 4   IE n/a   ECMA n/a

Returns the value of the first item in an array and removes it from the array. The length of the array decreases by one.

 
Returned Value
Any JavaScript value.
 
Parameters
None.
slice()NN 4   IE J3   ECMA n/a
slice(startIndex[, endIndex])  
 

Returns an array that is a subset of contiguous items from the main array. Parameters determine where the selection begins and ends.

 
Returned Value
An Array object.
 
Parameters
startIndex A zero-based integer of the first item of the subset from the current array.
endIndex An optional zero-based integer of the last item of the subset from the current array. If omitted, the selection is made from the startIndex position to the end of the array.
sort()NN 3   IE J3   ECMA 1
sort([compareFunction]) 
 

Sorts the values of the array either by the ASCII value of string versions of each array entry or according to a comparison function of your own design. The sort() method repeatedly invokes the comparison function, passing two values from the array. The comparison function should return an integer value, which is interpreted by the sort() function as follows:

Value

Meaning

<0

The second passed value should sort above the first value.

0

The sort order of the two values should not change.

>0

The first passed value should sort above the second.

The following comparison function sorts values of an array in numerical (instead of ASCII) order:

function doCompare(a, b) {
    return a - b
}

To sort an array by this function, the statement is:

myArray.sort(doCompare)

By the time the sort() method has completed its job, it has sent all values to the doCompare() function two values at a time and sorted the values on whether the first value is larger than the second.

Not only does the sort() method rearrange the values in the array, but it also returns a copy of the sorted array.

 
Returned Value
An Array object.
 
Parameters
compareFunction A reference to a function that receives two parameters and returns an integer result.
unshift()NN 4   IE n/a   ECMA n/a
unshift(value) 
 

Inserts an item at the beginning of an array. The length of the array increases by one, and the method returns the new length of the array.

 
Returned Value
Integer.
 
Parameters
value Any JavaScript value.