JavaScript Array pop() Method
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. If the array is empty, `pop()` returns undefined, ensuring no errors occur.
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', 'w3wiki'];
// Popping the last element from the array
console.log(arr.pop());
}
func();
Output
w3wiki
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
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers:
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.