The pop() method of the Array class in JavaScript removes the last element from an array and returns that element.
The syntax of Array.pop() method is:
pop()
This pop() method of Array removes the last element from an array and returns that element.
Array.pop() returns value of type Any
.
In JavaScript, we can use the pop() method to remove and return the last element from an array.
For example,
const arr = [1, 2, 3, 4, 5];
const removedElement = arr.pop();
console.log(removedElement);
5
If the array is empty, pop() method returns undefined.
For example,
const emptyArr = [];
const result = emptyArr.pop();
console.log(result);
undefined
In this JavaScript tutorial, we learned about pop() method of Array: the syntax and few working examples with output and detailed explanation for each example.