What is array.pop() in JavaScript?

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.

Syntax

array.pop()

Parameter(s)

This method does not accept any parameters.

Return value

The pop() method returns the removed element.

Example

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

Free Resources