Export (0) Print
Expand All

push Method (JavaScript)

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

arrayObj.push([item1 [item2 [. . . [itemN ]]]])

arrayObj

Required. An Array object.

item, item2,. . ., itemN

Optional. New elements of the Array.

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.

The following example illustrates the use of the push method.

var number;
var my_array = new Array();

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

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

// The preceding code displays the following: 9 8 7 6 5

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards. See Version Information.

Applies To: Array Object (JavaScript)

Show:
© 2014 Microsoft