Array.prototype.pop()
The pop()
method removes the last element from an array and returns that element. This method changes the length of the array.
Syntax
pop()
Return value
The removed element from the array; undefined
if the array is empty.
Description
The pop
method removes the last element from an array and returns that value to the caller.
pop
is intentionally generic; this method can be called or applied to objects resembling arrays. Objects which do not contain a length
property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.
If you call pop()
on an empty array, it returns undefined
.
Array.prototype.shift()
has similar behavior to pop
, but applied to the first element in an array.
Examples
Removing the last element of an array
The following code creates the myFish
array containing four elements, then removes its last element.
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon']; var popped = myFish.pop(); console.log(myFish); // ['angel', 'clown', 'mandarin' ] console.log(popped); // 'sturgeon'
Using apply( ) or call ( ) on array-like objects
The following code creates the myFish
array-like object containing four elements and a length parameter, then removes its last element and decrements the length parameter.
var myFish = {0:'angel', 1:'clown', 2:'mandarin', 3:'sturgeon', length: 4}; var popped = Array.prototype.pop.call(myFish); //same syntax for using apply( ) console.log(myFish); // {0:'angel', 1:'clown', 2:'mandarin', length: 3} console.log(popped); // 'sturgeon'
Using an object in an array-like fashion
push
and pop
are intentionally generic, and we can use that to our advantage — as the following example shows.
Note that in this example, we don't create an array to store a collection of objects. Instead, we store the collection on the object itself and use call
on Array.prototype.push
and Array.prototype.pop
to trick those methods into thinking we’re dealing with an array.
const collection = { length: 0, addElements: function(...elements) { // obj.length will be incremented automatically // every time an element is added. // Returning what push returns; that is // the new value of length property. return [].push.call(this, ...elements); }, removeElement: function() { // obj.length will be decremented automatically // every time an element is removed. // Returning what pop returns; that is // the removed element. return [].pop.call(this); } } collection.addElements(10, 20, 30); console.log(collection.length); // 3 collection.removeElement(); console.log(collection.length); // 2
Specifications
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
pop |
1 |
12 |
1 |
5.5 |
4 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
See also
Array.prototype.push()
Array.prototype.shift()
Array.prototype.unshift()
Array.prototype.concat()
Array.prototype.splice()
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop