Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Array.prototype.pop

Removes the last element of an array and returns it.

Available since: ES3

Syntax

array.pop()

Return value

anyThe removed element, or undefined if the array is empty.

Examples

Taking off the last element:

const stack = ['a', 'b', 'c'];
const last = stack.pop();
 
console.log(last);
console.log(stack);

Output:

c
[
  "a",
  "b"
]

An empty array:

console.log([].pop());

Output:

undefined

Notes

The method changes the array itself. Together push() and pop() turn an array into a stack: last in, first out.

See also

In the tutorial

  • Arrays — What an array is in JavaScript: creating one, indexes, length, changing elements and nested arrays.

Thanks for reading!

Donate to the site — we work hard for you.