Visit Mozilla.org

Core JavaScript 1.5 Reference:Global Objects:Array:push

From MDC


Contents

[edit] Summary

Adds one or more elements to the end of an array and returns the new length of the array. This method changes the length of the array.

Method of Array
Implemented in: JavaScript 1.2, NES 3.0

JavaScript 1.3: push returns the new length of the array rather than the last element added to the array.

ECMA Version: ECMA-262 Edition 3

[edit] Syntax

var newlen = array.push(element1, ..., elementN);

[edit] Parameters

element1, ..., elementN 
The elements to add to the end of the array.

[edit] Description

The behavior of the push method is analogous to the push function in Perl 4. Note that this behavior is different in Perl 5.

[edit] Backward Compatibility

[edit] JavaScript 1.2

The push method returns the last element added to an array.

[edit] Examples

[edit] Example: Adding elements to an array

The following code creates the myFish array containing two elements, then appends two elements to it. After the code executes, pushed contains 4. (In JavaScript 1.2, pushed contains "lion" after the code executes.)

var myFish = ["angel", "clown"];
var pushed = myFish.push("drum", "lion");

[edit] See Also

pop, shift, unshift