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 arrays let 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 arrays let A = array1.pop(); let B = array2.pop(); let C = array3.pop(); // print popped values console.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 array let emptyArray = [] // save popped element let pop = emptyArray.pop(); // log out popped element console.log(pop) // prints undefined
RELATED TAGS
CONTRIBUTOR
View all Courses