Mastering the pop() Method in JavaScript

Introduction

The pop() method in JavaScript is used to remove the last element from an array and return that element. It’s the opposite of push() and is commonly used when you want to work with arrays in a Last-In-First-Out (LIFO) manner. 🧠

📌 What is pop()?

pop() modifies the original array by removing the last element and returns the removed value.

  • 📦 Works only with arrays.
  • ➖ Removes the last element from the array.
  • 🔄 Changes the original array (mutates it).

💡 Syntax

Basic Syntax

array.pop()

🧵 Removing the Last Element

Pop Single Element

const fruits = ["Apple", "Banana", "Mango"];
const lastFruit = fruits.pop();
console.log(lastFruit); // "Mango"
console.log(fruits);    // ["Apple", "Banana"]

📏 Return Value

The pop() method returns the removed element. If the array is empty, it returns undefined.

Return Value of pop()

const emptyArr = [];
console.log(emptyArr.pop()); // undefined

⚠️ Important Notes

Note

pop() changes the original array. If you want to keep the original intact, you can create a copy before popping.

Immutable Alternative

const arr = [1, 2, 3];
const copy = [...arr];
copy.pop();
console.log(copy); // [1, 2]
console.log(arr);  // [1, 2, 3]

📊 Quick Reference Table

OperationCodeResult
Pop Singlearr.pop()Removes last element
Return Valuelet item = arr.pop()Returns removed element
Empty Array[].pop()Returns undefined
>>"If push adds to the end, pop takes from the end — the perfect pair for managing lists dynamically." 🔄

🔥 Summary

The pop() method is essential when you need to remove elements from the end of an array. It’s simple, fast, and works perfectly alongside push() for stack-like operations in JavaScript.