The JavaScript Array pop() Method is used to remove the last element of the array and also returns the removed element. This function decreases the length of the array.
Syntax:
arr.pop()
Parameters: 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.
Below is an example of the Array pop() method.
Example 1:
JavaScript
function func() {
let arr = [ 'GFG' , 'gfg' , 'g4g' , 'GeeksforGeeks' ];
console.log(arr.pop());
}
func();
|
Output:
GeeksforGeeks
Example 2: In this example, the pop() method removes the last element from the array, which is 4, and returns it.
JavaScript
function func() {
let arr = [34, 234, 567, 4];
let popped = arr.pop();
console.log(popped);
console.log(arr);
}
func();
|
Output:
4
34,234,567
Example 3: In this example, the function pop() tries to extract the last element of the array but since the array is empty therefore it returns undefined as the answer.
JavaScript
function func() {
let arr = [];
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: The browsers supported by the JavaScript Array pop() method are listed below:
- Google Chrome 1.0 and above
- Microsoft Edge 12 and above
- Mozilla Firefox 1.0 and above
- Safari 1 and above
- Opera 4 and above
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 May, 2023
Like Article
Save Article