JavaScript push() Method Array.push() method

This tutorial covers the push() method, part of JavaScript's Array object. The push() method adds one or more elements to the end of an array.

Related array method:
The pop() method removes elements from an array.

Using the push() Method

In the script below, we will add two new elements to an array with the push() method, and see what value is returned.

var arlene = new Array("alpha", "beta", "gamma");

// Add elements with the push() method
alert( arlene.push("delta","epsilon") );

The push() method added two elements to our array, and returned the length of the modified array (after having added the elements passed as arguments). The alert in our last line of script confirms the new array size.
JavaScript push() method

Calling the push() method without argument will not affect the array (since elements to add come from arguments passed). As a result, push() would simply return the size of the array (the array's length property).

Test the push() Method

Interactively test the push() method by editing the JavaScript code below and clicking the Test push() Method button.

Browser support for JavaScript push() method
Internet Explorer supports the JavaScript push() methodFirefox supports the JavaScript push() methodSafari supports the JavaScript push() methodOpera supports the JavaScript push() method