We're live-coding on Twitch! Join us!

Basics

This method takes out the last element from an array and returns it. The pop method mutates the array during this process reducing its length.

['me', 'him', 'you'].pop()

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.

Syntax

array.pop()

No Parameters

Returns a string

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

const names = ['Johnny', 'Pete', 'Sammy']
console.log(names.pop());
// output: 'Sammy'

Common Uses and Snippets

Removing the last element of an array

The pop method is mostly used to remove the last element of an array and output both the parent array and the value removed.

Like this article? Follow @codebeast on Twitter