★ wanayoo — archive 1999 http://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/pushNouvelle recherche | Portail wanayoo

Mozilla.com

push

Summary

Mutates an array by appending the given elements and returning the new 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

Syntax

array.push(element1, ..., elementN)

Parameters

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

Description

The push method is useful for easily appending values to an array. The return value of this method is the new length property of the object upon which the method was called.

push is intentionally generic. This method can be called or applied to objects resembling arrays. The push method relies on a length property to determine where to start inserting the given values. If the length property cannot be converted into a number, the index used is 0. This includes the possibility of length being nonexistent, in which case length will also be created.

The only native, array-like objects are strings, although they are not suitable in applications of this method, as strings are immutable.

Examples

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.

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

See Also

Page last modified 05:55, 18 Jul 2008 by Sevenspade

Tags:

Files (0)