Share via


push Method

Appends new elements to an array, and returns the new length of the array.

function push([item1 : Object [, ... [, itemN : Object]]]) : Number

Arguments

  • item1, ... , itemN
    Optional. New elements of the Array.

Remarks

The push and pop methods enable you to simulate a stack, which uses the principle of last in, first out (LIFO) to store data.

The push method appends elements in the order in which they appear. If one of the arguments is an array, it is added as a single element. Use the concat method to join the elements from two or more arrays.

Example

The following example illustrates the use of the push method.

var my_array = new Array();

my_array.push (5, 6, 7);
my_array.push (8, 9);

var s = "";
var number;

number = my_array.pop();
while (number != undefined)
    {
    s += number + " ";
    number = my_array.pop();
    }

print (s);
// Output: 9 8 7 6 5

Requirements

Version 5.5

Applies To:

Array Object

See Also

Reference

concat Method (Array)

pop Method

Change History

Date

History

Reason

July 2009

Modified example.

Content bug fix.

March 2009

Added example.

Information enhancement.