Open In App

JavaScript Array pop() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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.

 Array pop() Method Syntax

arr.pop();
  • This method does not accept any parameter.

Array pop() Method Return value

This method returns the removed element array. If the array is empty, then this function returns undefined.

Array pop() Method Examples

Example 1: Removing the Last Element from the Array

The pop() method in JavaScript removes the last element from an array and returns that element. In this example, it removes and prints the last element ‘GeeksforGeeks’ from the array [‘GFG’, ‘gfg’, ‘g4g’, ‘GeeksforGeeks’].

JavaScript
function func() {
    let arr = ['GFG', 'gfg', 'g4g', 'GeeksforGeeks'];

    // Popping the last element from the array
    console.log(arr.pop());
}
func();

Output:

GeeksforGeeks

Example 2: Removing element from empty array

Here, 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 = [];

    // 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:

  • 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.


Last Updated : 12 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads