Array.prototype.pop()

Overview.

The Array.prototype.pop() method removes the last element from an array and returns that element. This method changes the length of the array.

Syntax

array.pop();

Parameters

No parameter.

return value

The removed element from the array, or undefined if the array is empty.

Examples

const fruits = ["apple", "banana", "orange"];
const lastFruit = fruits.pop();
// lastFruit is "orange"
// fruits is now ["apple", "banana"]

Notes

  • The pop() method modifies the original array.
  • If the array is empty, pop() returns undefined.
  • It is possible to use pop() together with push() to implement a basic stack data structure.

This page was updated on -