JavaScript » Objects » Array

Syntax:
new Array(arrayLength)
 
new Array(element0, element1, ..., elementN)

An array is an ordered set of values grouped together under a single variable name created by using an Array object constructor.

You can create an Array literal by specifying the name of the array and the values of all its elements.

If you create an array with a single numeric parameter, that number is stored in the length property, and the array doesn't actually have any elements until some are specifically assigned to it. If, however, the parameter is not a number, an array of 1 element is created and that value assigned to it. You can easily increase the size of an array by assigning a value to an element higher than its current length.
 
NOTE:
 
If you specify 'language="Javascript1.2"' in the <SCRIPT> tag and use a single numeric parameter with the Array constructor, it will be seen as the value of a single element of the array rather than the number of elements you want that array to contain.

Examples

Code:
cars = new Array("Mercedes", "Ford", "Chrysler")
Explanation:

This example creates an array of three elements. The elements of an array are indexed using their ordinal number, starting with 0. You could, therefore, refer to the second element in the above array ("Ford") as 'cars[1]'.

Code:
fruit = new Array(7)
Explanation:

You can specify the number of elements in a new array by using a single numeric parameter with the Array constructor. This example creates an array of 7 elements.

Properties

constructor

Syntax: Object.constructor

This specifies a function to create an object's property and is inherited by all objects from their prototype.

index

Syntax: object.index

The read-only index property for an array created by a regular expression match and containing the zero-based index of that match.

input

Syntax: object.input

The read-only input property for an array created by a regular expression match and containing the original string against which the match was made.

length

Syntax: object.length

The length property holds an unsigned 32 bit integer representing the length of the array. It can be altered independently of the number of elements in the array.

prototype

Syntax: Object.prototype.name = value

This allows the addition of properties and methods to any object.

Methods

concat

Syntax: Array.concat(arrayName2, arrayName3, ..., arrayNameN)

The concat method joins two or more Array objects producing one new one. The original Array objects are unaffected by this but, if one copy of a string or number is altered, it is not reflected in the other, whereas a change to an object reference can be seen in both copies.

eval

Syntax: Object.eval(string)

The eval method is deprecated as a method of Object, but is still used as a high level function. It evaluates a string of JavaScript in the context of an object.

join

Syntax: Array.join(separator)

The join method is used to join all the elements of an array into a single string separated by a specified string separator (if none is specified, the default is a comma).

pop

Syntax: Array.pop()

The pop method is used to remove and return the last element of an array. This affects the length of the array.

push

Syntax: Array.push(element1, ..., elementN)

The push method is used to add one or more elements to an array, returning the new length of it. This affects the length of the array.

reverse

Syntax: Array.reverse()

The reverse method, as the name implies, reverses the order of the elements in an array making the first last and the last first.

shift

Syntax: Array.shift()

The shift method removes and returns the first element of an array. This affects the length of the array.

slice

Syntax: Array.slice(begin[,end])

The slice method creates a new array from a selected section of an array.

sort

Syntax: Array.sort(compareFunction)

The sort method sorts the elements of an array.

splice

Syntax: Array.splice(index, howMany, [element1][, ..., elementN])

The splice method is used to add and/or remove elements of an array.

toSource

Syntax: Object.toSource()

The toSource method returns a literal representing the source code of an object. This can then be used to create a new object.

toString

Syntax: Object.toString()

The toString method returns a string representing a specified object.

unshift

Syntax: Array.unshift(element1,..., elementN)

The unshift method adds one or more elements to the beginning of an array and returns the new length.

unwatch

Syntax: Object.unwatch(property)

This method removes a watchpoint set for an object and property name with the watch method.

valueOf

Syntax: Object.valueOf()

This method returns a primitive value for a specified object.

watch

Syntax: Object.watch(property, handlerfunction)

This method adds a watchpoint to a property of the object.

See Also: