JavaScript 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.
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 | |||
---|---|---|---|