Start learning free

LESSON 102 OF 410 · ARRAYS I

Remove Last Item From Array in JavaScript

Lesson Read the full explanation

Another way to change the data in an array is with the .pop() method.

.pop() removes the last element of an array and also returns the value of that element:

let threeArr = [1, 4, 6];

// Remove last element:
let oneDown = threeArr.pop();

console.log(oneDown); // Output: 6
console.log(threeArr); // Output: [1, 4]

// Remove last element again:
let oneDown = threeArr.pop();

console.log(oneDown); // Output: 4
console.log(threeArr); // Output: [1]

Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    JAVASCRIPT · ARRAYS I

    Array Pop in JavaScript, explained

    JavaScript array.pop(): Remove and Return the Last Element

    Every stack, undo system, and depth-first search algorithm relies on removing the most recently added item. In JavaScript, pop() is the one-liner that does exactly that: removes the last element from an array and hands it back to you. Unlike many array methods, it both modifies the array and returns useful data — the removed element itself. Understanding this dual behavior is what separates confident array users from beginners who get surprised by unexpected results.

    How pop() Works

    const colors = ["red", "green", "blue"];
    
    const removed = colors.pop();
    
    console.log(removed);   // "blue"    — the removed element
    console.log(colors);    // ["red", "green"]  — array is now shorter
    

    Two things happen in one call: the last element is removed from colors, and it's returned so you can use it. If you don't need the removed value, you can ignore the return: arr.pop();

    Popping from an Empty Array

    const empty = [];
    const result = empty.pop();
    
    console.log(result);   // undefined — no error thrown
    console.log(empty);    // []
    

    JavaScript never throws on pop() — it just returns undefined for an empty array. Guard against this when the returned value matters:

    if (stack.length > 0) {
      const item = stack.pop();
      // safe to use item
    }
    

    The Stack Pattern: push() + pop()

    A stack is a Last-In-First-Out (LIFO) structure — the last item added is the first item removed. This powers undo/redo, browser history, call stacks, and DFS graph traversal:

    // Undo system:
    const history = [];
    
    function doAction(action) {
      history.push(action);
      console.log(`Did: ${action}`);
    }
    
    function undo() {
      const lastAction = history.pop();
      if (lastAction) {
        console.log(`Undid: ${lastAction}`);
      } else {
        console.log("Nothing to undo");
      }
    }
    
    doAction("type 'Hello'");
    doAction("bold text");
    doAction("insert image");
    
    undo();   // Undid: insert image
    undo();   // Undid: bold text
    console.log(history);  // ["type 'Hello'"]
    

    pop() vs. Other Removal Methods

    • pop() — removes the last element; O(1) — very fast.
    • shift() — removes the first element; O(n) — slower because every other element shifts down.
    • splice(i, 1) — removes element at index i; O(n) — elements after index shift down.
    const arr = [1, 2, 3, 4, 5];
    
    arr.pop();     // removes 5  → [1, 2, 3, 4]
    arr.shift();   // removes 1  → [2, 3, 4]
    arr.splice(1, 1);  // removes 3  → [2, 4]
    

    Key Takeaways

    • pop() removes the last element, returns it, and mutates the original array.
    • On an empty array, pop() returns undefined without throwing.
    • pop() is O(1) — instant regardless of array size. Use it instead of splice(-1, 1).
    • Paired with push(), it implements a stack — one of the most useful data structures in programming.

    What to Learn Next

    With pop() clear, you have the full set of array mutation methods: push()/pop() for the end, unshift()/shift() for the front. Next, explore queues (FIFO structures using push() and shift()) and see how arrays underpin both stacks and queues. Then dive into array destructuring and the spread operator for immutable alternatives to these mutation methods.

    Keep going with JavaScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free