Array Functions - Usage

The following material is borrowed very heavily from the reference section of David Flanagan's Javascript: The Definitive Guide (an excellent book, I might add).

Array.concat()

Synopsis

array.concat(value,...)

Arguments

value,...

One or more values to be appended to the end of array.

Returns

A new array that is formed by concatenating each of the specified arguments to array

.

Description

concat() creates and returns a new array which is the result of concatenating each of the method's arguments to array. It does not modify array.

Array.copy()

Synopsis

array.copy()

Returns

A new array that contains the same elements as array

.

Description

copy() creates and returns a new array which contains the same elements as array. It does not modify array. Numerical and boolean array elements are copied by value while all other types are copied by reference.

Array.pop()

Synopsis

array.pop()

Returns

The last element of array.

Description

pop() removes the last element of array (the one with the highest index) and returns this value. array is modified.

Array.push()

Synopsis

array.push(value,...)

Arguments

value,...

One or more values to be appended to the end of array.

Returns

The new length of array.

Description

push() appends each of it's arguments, in the order they appear in the function call, to array. array is modified.

Array.shift()

Synopsis

array.shift()

Returns

The first element of array.

Description

shift() removes the first element of array (the one with index 0) and returns this value. array is modified and all remaining array elements are moved on index down to fill the newly vacated spot.

Array.slice()

Synopsis

array.slice(start, end)

Arguments

start

The array index in array at which to start the slice. The element at this position will be at index 0 in the new array. If this value is negative, it specifies a position measured from the end of the array (ie. -1 is the last element).

end

The array index in array immediately after the end of the slice. The element at end - 1 will occupy the last position in the new array. If this argument is omitted, then the slice will contain all elements from start to the end of the array. If end is a negative number, it indicates an array position measured from the end of the array.

Returns

An array containing all the elements in the specified slice of array.

Description

slice() creates a new array containing all the elements between the positions indicated by start and end excluding the element at the position specified by end. It does not modify array.

Note that a bug in Internet Explorer 4 causes negative start values to be interpreted as 0 in the core version of the Array.slice() function implemented in that browser. This function does not suffer from the same problem.

Array.splice()

Synopsis

array.splice(start, deleteCount, value, ...)

Arguments

start

The array index in array at which to start the insertion / deletion of elements.

deleteCount

The number of elements to delete from array. This includes the element at start. If this argument is omitted, all elements from the one at start to the end of the array are deleted.

value,...

One or more values to be inserted into array at the index specified by start.

Returns

An array containing all the elements deleted from array.

Description

splice() deletes zero or more elements from array starting at start and replaces them with zero or more elements specified in the argument list. array is modified with any remaining elements being moved to different array indices if required.

Note that a bug in Netscape 4.x causes the implementation of this function to return an element value instead of an array if only a single element is deleted. Netscape 4.x will also return nothing if no elements are deleted instead of an empty array. This function does not suffer from the same problems.

Array.unshift()

Synopsis

array.unshift(value,...)

Arguments

value,...

One or more values to be inserted at the beginning of array.

Returns

The new length of array.

Description

unshift() insert each of it's arguments, in the order they appear in the function call, at the beginning of array. array is modified with existing array elements moving to positions with higher indices.

More about this script

Licensing

This script is released under a Creative Commons License.