The pop()
method removes the last element of an array. It returns the element that it removed. The pop()
method changes the length of the array by decreasing the array.
array.pop()
This method does not accept any parameters.
The pop()
method returns the removed element.
In the example below, we will create some arrays and use the pop()
method.
// create arrayslet array1 = [1, 2, 3, 4, 5]let array2 = ["e", "d", "p", "r", "e", "s", "s", "o", "o"];let array3 = ["a", "b", "c", "d", "e"]// popped arrayslet A = array1.pop();let B = array2.pop();let C = array3.pop();// print popped valuesconsole.log(A);console.log(B);console.log(C)
Note: The
pop
array, when called on an empty array, returns undefined.
See the example below.
// create empty arraylet emptyArray = []// save popped elementlet pop = emptyArray.pop();// log out popped elementconsole.log(pop) // prints undefined