The Wayback Machine - https://web.archive.org/web/20250401085759/https://www.geeksforgeeks.org/javascript-array-pop-method/
Open In App

JavaScript Array pop() Method

Last Updated : 11 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.

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: In this example this func() function initializes an empty array arr and attempts to remove the last element using the pop() method.

JavaScript
function func() {
    let arr = [];

    // popping the last element
    let popped = arr.pop();
    console.log(popped);
}
func();

Output
undefined

Supported Browsers:

JavaScript Array pop() Method- FAQs

How does the pop() method work?

The pop() method removes the last element from an array and returns the removed element. If the array is empty, pop() returns undefined

What happens if the array is empty when using pop()?

If the array is empty, the pop() method returns undefined, as there are no elements to remove.

Is pop() efficient for large arrays?

The pop() method is generally efficient because it only removes the last element of the array, which is an O(1) operation. However, its performance is influenced by the underlying implementation of the JavaScript engine.

How does pop() handle sparse arrays?

If the last element in a sparse array is undefined, pop() will remove that undefined element and the length of the array will be decreased by one.

What are some common use cases for the pop() method?

Common use cases for the pop() method include:

  • Removing the last item from a list or stack.
  • Implementing stack-like behavior where elements are added and removed from the end.
  • Modifying the array dynamically based on certain conditions.

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

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.


Next Article

Similar Reads

three90RightbarBannerImg