JavaScript Array pop() method

The JavaScript array pop() method removes the last element from the given array.

pop() returns the element it removed.

JavaScript Array pop() method changes the length of the original array.

If you call pop() on an empty array, it returns undefined.

This function decreases the length of the array by 1.

In this tutorial, you will learn about the JavaScript Array pop() method with the help of examples.

This method changes the original array and its length.

Syntax

array.pop()

Parameters

None.

Return Value

The removed element from the array; undefined if the array is empty. A string, a number, an array, or any other type allowed in an array.

Examples

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"]
var myFish = ['angel', 'clown', 'mandarin', 'sturgeon'];

var popped = myFish.pop();

console.log(myFish); // ['angel', 'clown', 'mandarin' ]

console.log(popped); // 'sturgeon'
// List of names
const students = ['william', 'john', 'chris', 'mike']

// Remove the last name
const newStudents = students.pop()

// Output the removed name
console.log(newStudents) // mike

This method when called:

  • Removes the element at the last index of the array.
  • Mutates the parent array reducing the length.
  • Returns the last element.
const names = ['Johnny', 'Pete', 'Sammy']
console.log(names.pop());
// output: 'Sammy'
// List of items
let items = ['Cedar', 'Fruits', 'Table'];

// Remove last item in list
let newItems = items.pop();

// Output modified list
console.log(items); // ["Cedar","Fruits"]

// Output removed item
console.log(newItems) // Table
let cities = ["Madrid", "New York", "Kathmandu", "Paris"];

// remove the last element
let removedCity = cities.pop();

console.log(cities)         // ["Madrid", "New York", "Kathmandu"]
console.log(removedCity);   // Paris
var languages = ["JavaScript", "Python", "Java", "C++", "Lua"];

var popped = languages.pop();

console.log(languages); // [ 'JavaScript', 'Python', 'Java', 'C++' ]
console.log(popped); // Lua

// pop returns any type of object
var numbers = [
  [1, 2, 3],
  [4, 5, 6],
  [-5, -4, -3],
];
console.log(numbers.pop()); // [ -5, -4, -3 ]
console.log(numbers); // [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]