JavaScript Array pop() Method
Last Updated :
11 Sep, 2024
Improve
The pop() method removes (pops) the last element of an array. The pop() method changes the original array.
It reduces the array’s length by one and returns the removed element.
Syntax:
arr.pop();
- This method does not accept any parameter.
Return value:
- This method returns the removed element array. If the array is empty, then this function returns undefined.
Example 1: In this example, This function func() initializes an array arr, then removes and logs its last element using the pop() method.
function func() {
let arr = ['GFG', 'gfg', 'g4g', 'GeeksforGeeks'];
// Popping the last element from the array
console.log(arr.pop());
}
func();
Output
GeeksforGeeks
Example 2: In this example this func() function initializes an empty array arr and attempts to remove the last element using the pop() method.
function func() {
let arr = [];
// popping the last element
let popped = arr.pop();
console.log(popped);
}
func();
Output
undefined