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:
undefinedNotes
The method changes the array itself. Together push() and pop() turn an array into a stack: last in, first out.
See also
- Array.prototype.push — Appends elements to the end of an array.
- Array.prototype.shift — Removes the first element of an array and returns it.
- Array.prototype.at — Returns the element at an index; negative indexes are allowed.
In the tutorial
- Arrays — What an array is in JavaScript: creating one, indexes, length, changing elements and nested arrays.