JavaScriptArraysBeginner

Array.pop()

Removes and returns the last element of an array (mutates).

Review the syntaxStudy the examplesOpen the coding app
array.pop()

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Removes and returns the last element of an array (mutates).

The pop() method removes the last element from an array and returns that value to the caller. This operation mutates the original array, decreasing its length by one. If the array is empty, the method returns undefined and the array remains unchanged. In terms of performance, pop() is an O(1) operation because it does not require re-indexing the remaining elements, making it significantly faster than shift(). It is commonly used in conjunction with push() to implement LIFO (Last-In, First-Out) stack data structures. The method is generic; it can be called or applied to objects resembling arrays, provided they have a length property and integer-keyed properties.

Quick reference

Syntax

array.pop()

See it in practice

Examples

1

Basic Stack Operation

var items = ['apple', 'banana', 'cherry'];
var lastItem = items.pop();
console.log(lastItem);
console.log(items);
Output:
"cherry" ["apple", "banana"]

Removes the last element from the array and returns it, mutating the original array.

2

Handling Empty Arrays

var empty = [];
var result = empty.pop();
console.log(result);
console.log(empty.length);
Output:
undefined 0

Calling pop() on an empty array does not throw an error; it returns undefined and the length remains 0.

3

Using pop() on Array-like Objects

var myObject = { 0: 'a', 1: 'b', 2: 'c', length: 3 };
var removed = Array.prototype.pop.call(myObject);
console.log(removed);
console.log(myObject);
Output:
"c" { 0: "a", 1: "b", length: 2 }

Using Function.prototype.call, pop() can manipulate objects that have a length property and numeric keys.

Debug faster

Common Errors

1

LogicError

Cause: Attempting to remove the last element from a state array in frameworks like React or Redux by mutating the source directly.

Fix: Use the spread operator or slice() to create a copy before modifying, as pop() mutates the reference.

var newState = oldState.slice(0, -1); // Immutable alternative to pop()
2

TypeError

Cause: Calling pop() on a variable that is null or undefined.

Fix: Initialize variables as empty arrays or use optional chaining or a null check before calling the method.

var list = null;
// Error: list.pop();
if (list) list.pop();

Runtime support

Compatibility

Node.js, BrowserAll browsersES3+

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Removes and returns the last element of an array (mutates).

LogicError: Use the spread operator or slice() to create a copy before modifying, as pop() mutates the reference. TypeError: Initialize variables as empty arrays or use optional chaining or a null check before calling the method.