JavaScript pop() Method
This tutorial explains the pop() method, from JavaScript's Array object. The pop() method is used to remove the last element of an array.
Related array methods:
The shift() method removes the first array element.
The push() method adds elements to an array.
Using the pop() Method
var arlene = new Array("alpha", "beta", "gamma");
// Use the pop() method to remove the last element
alert( arlene.pop() +"\n"+ arlene.length );
The script above calls the pop() method on the arlene array, and displays the resulting length (size) of the array on a new line. The value of the last array element is returned on the first line; the second line reads "2", one less than the original array since the last element has been removed.
The pop() method does not accept any argument. Executing the pop() method on an empty array returnes "undefined" (no removable element to return).
Test the pop() Method
Interactively test the pop() method by editing the JavaScript code below and clicking the Test pop() Method button.
Browser support for JavaScript pop() method | |||
---|---|---|---|