The pop() method is used to remove the last element from an array.

  • Removes the last element of an array
  • Modifies the original array
  • Returns the removed element
  • If the array is empty, it returns undefined

1.Syntax

array.pop();

2.Basic Example

let fruits = ["Apple", "Banana", "Mango"];

let removedItem = fruits.pop();

console.log(removedItem); // Mango
console.log(fruits); // ["Apple", "Banana"]

3.Return Value of pop()

let numbers = [10, 20, 30];

let lastNumber = numbers.pop();

console.log(lastNumber); // 30
console.log(numbers); // [10, 20]

4.pop() on an Empty Array

let arr = [];

console.log(arr.pop()); // undefined

⚠ No error occurs.

5.Using pop() in a Loop

let stack = [1, 2, 3, 4];

while (stack.length > 0) {
console.log(stack.pop());
}

Output

4
3
2
1

Categorized in:

Javascript Array Methods,