Array.prototype.pop()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Thepop()
method ofArray
instances removes thelastelement from an array and returns that element. This method changes the length of thearray.
Try it
const plants = ["broccoli", "cauliflower", "cabbage", "kale", "tomato"];console.log(plants.pop());// Expected output: "tomato"console.log(plants);// Expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]plants.pop();console.log(plants);// Expected output: Array ["broccoli", "cauliflower", "cabbage"]
Syntax
pop()
Parameters
None.
Return value
The removed element from the array;undefined
if the array is empty.
Description
Thepop()
method removes the last element from an array and returns that value to the caller. If you callpop()
on an empty array, it returnsundefined
.
Array.prototype.shift()
has similar behavior topop()
, but applied to the first element in an array.
Thepop()
method is a mutating method. It changes the length and the content ofthis
. In case you want the value ofthis
to be the same, but return a new array with the last element removed, you can usearr.slice(0, -1)
instead.
Thepop()
method isgeneric. It only expects thethis
value to have alength
property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
Examples
Removing the last element of an array
The following code creates themyFish
array containing four elements, thenremoves its last element.
const myFish = ["angel", "clown", "mandarin", "sturgeon"];const popped = myFish.pop();console.log(myFish); // ['angel', 'clown', 'mandarin' ]console.log(popped); // 'sturgeon'
Calling pop() on non-array objects
Thepop()
method reads thelength
property ofthis
. If thenormalized length is 0,length
is set to0
again (whereas it may be negative orundefined
before). Otherwise, the property atlength - 1
is returned anddeleted.
const arrayLike = { length: 3, unrelated: "foo", 2: 4,};console.log(Array.prototype.pop.call(arrayLike));// 4console.log(arrayLike);// { length: 2, unrelated: 'foo' }const plainObj = {};// There's no length property, so the length is 0Array.prototype.pop.call(plainObj);console.log(plainObj);// { length: 0 }
Using an object in an array-like fashion
push
andpop
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 usecall
onArray.prototype.push
andArray.prototype.pop
to trick those methods into thinking we're dealing with an array.
const collection = { length: 0, addElements(...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() { // 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); // 3collection.removeElement();console.log(collection.length); // 2
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.pop |