The Wayback Machine - https://web.archive.org/web/20240915120703/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
Save
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.


Previous Article
Next Article

Similar Reads

Shift() vs pop() Method in JavaScript
JavaScript shift() and pop() methods are used to remove an element from an array. But there is a little difference between them. The shift() method removes the first element and whereas the pop() method removes the last element from an array. Javascript Array shift() method: The JavaScript Array shift() Method removes the first element of the array
2 min read
What is the pop() method used for in JavaScript Arrays ?
The pop() method in JavaScript is used to remove the last element from an array. It alters the original array by removing and returning the last element. This can be handy when you need to manipulate arrays by taking elements off the end. Example: Here, pop() not only removes the last element (4) from the array but also returns it, allowing you to
1 min read
TypeScript Array pop() Method
The TypeScript Array.pop() method removes the last element from an array and returns it. This operation modifies the original array by reducing its length by one. If the array is empty, pop() returns undefined without making any changes to the array. Syntax: array.pop(); Parameter: This method does not accept any parameter. Return Value: This metho
2 min read
How to create a pop-up to print dialog box using JavaScript?
Given an HTML document and the task is to design a button that would pop-up a print dialog box. We are going to use JavaScript to do the assigned task: Approach:: Add a button which links to a JavaScript Function. Inside the JavaScript Function, use the JavaScript default function to call the print dialog box Syntax: window.print Example: <!DOCT
2 min read
How to Replace a JavaScript Alert Pop Up with a Fancy Alert Box ?
JavaScript alert popups are simple and effective for displaying messages to users, but they lack customization and can be visually unappealing. Below are the approaches to replace a JavaScript alert pop-up with a fancy alert box. Table of Content By overriding the alert functionUsing a SweetAlert LibraryBy overriding the alert functionIn this appro
3 min read
How to Replace a JavaScript Alert Pop up with a fancy Alert Box ?
JavaScript alert popups are an easy and effective way to show users messages, but they are not very customizable and don't always look good. Several methods exists in JavaScript to replace a JavaScript alert pop-up with a fancy alert box which are as follows: Table of Content Using Custom HTML and CSSUsing a SweetAlert LibraryUsing CSS FrameworksUs
3 min read
How to use push() & pop() in JavaScript Arrays?
Arrays are dynamic data structures in JavaScript that have the ability to hold several values of various types. Push() and pop() are used for adding and removing elements from an array's last index. Below are the approaches to use push() and pop() methods in JavaScript: Table of Content Basic UsageConditional UsageApproach 1: Basic UsageIn this app
2 min read
Collect.js pop() Method
The pop() method is used to remove the last element from collection and returns the popped element. Syntax: collect(array).pop() Parameters: The collect() method takes one argument that is converted into the collection and then pop() method is applied on it. Return Value: This method returns the popped element from given collection. Below example i
1 min read
PHP | Ds\Deque pop() Function
The Ds\Deque::pop() function is an inbuilt function in PHP which is used to remove the last element from Deque (if Deque is not empty) and return it. If Deque is empty then it throws an exception. Syntax: public Ds\Deque::pop( void ) : mixed Parameters: This function does not accept any parameter. Return Value: This function returns the removed las
2 min read
PHP | Ds\Sequence pop() Function
The Ds\Sequence::pop() function is an inbuilt function in PHP which removes the last value from sequence and return it. Syntax: abstract public Ds\Sequence::pop( void ) : mixed Parameters: This function does not accept any parameter. Return Value: This function returns the popped value. Below programs illustrate the Ds\Sequence::pop() function in P
1 min read
PHP | Ds\Vector pop() Function
The Ds\Vector::pop() function is an inbuilt function in PHP which is used to remove the last element of a vector and return it. Syntax: mixed public Ds\Vector::pop( void ) Parameters: This function does not accept any parameter. Return Value: This function returns the last element which removed from the vector. Below programs illustrate the Ds\Vect
2 min read
PHP | Ds\Stack pop() Function
The Ds\Stack::pop() function of PHP is used to remove the element present at the top of the Stack instance. This function also returns the top element of the stack after removing it. Syntax: mixed public Ds\Stack::pop ( void ) Parameters: This function does not accept any parameters.Return Value mixed This function returns the element present at th
2 min read
PHP Ds\PriorityQueue pop() Function
The Ds\PriorityQueue::pop() Function in PHP is used to remove and return the value present at the top of the PriorityQueue. In other words, it returns the value with the highest priority in the PriorityQueue and removes it. Syntax: mixed public Ds\PriorityQueue::pop ( void ) Parameters: This function does not accepts any parameters. Return Value: T
2 min read
PHP Ds\Queue pop() Function
The Ds\Queue::pop() Function in PHP is used to remove and return the value present at the top of the Queue. In other words, it returns the value present at the front of the Queue and also removes it from the Queue. Syntax: mixed public Ds\Queue::pop ( void ) Parameters: This function does not accepts any parameters. Return Value: This function retu
2 min read
PHP SplDoublyLinkedList pop() Function
The SplDoublyLinkedList::pop() function is an inbuilt function in PHP which is used to pop the node from the end of the doubly linked list. Syntax: mixed SplDoublyLinkedList::pop( void ) Parameters: This function does not accept any parameters. Return Value: This function returns the value of a popped node in the doubly linked list. The below progr
1 min read
p5.js | Pop Operation in Stack
The stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). Pop Operation on Stacks: Accessing the content while removing it from the top of the stack, is known as a Pop Operation. In an array implementation of pop() operation, the
4 min read
Node.js pop() function
pop() is an array function from Node.js that is used to remove elements from the end of an array. Syntax: array_name.pop() Parameter: This function does not take any parameter. Return type: The function returns the array. The program below demonstrates the working of the function: Example 1: C/C++ Code function POP() { arr.pop(); console.log(arr);
1 min read
PHP | ImagickDraw pop() Function
The ImagickDraw::pop() function is an inbuilt function in PHP which is used to destroy the current ImagickDraw in the stack and returns the previously pushed ImagickDraw. For every pop() function there must have already been an equivalent push() function. Syntax: bool ImagickDraw::pop( void ) Parameters: This function doesn’t accepts any parameter.
2 min read
How to create a pop-up div on mouse over and stay when click using jQuery ?
In this article, we will learn how to create a pop-up div on mouseover and stay when click using jQuery. Approach: First, we create an HTML div element that we want to pop up when we mouse over on an element and set its display property to none in CSS style.display:none;In the script tag, we create a variable flag and set its value to -1. $flag = -
2 min read
Backbone.js pop Collection
In this article, we will discuss pop collection The Backbone.js pop collection is used to remove a model or an array of models from the given collection. It is similar to the remove method. Syntax: collection.pop(models,options) Parameters: It will take two parameters. models: this is the first parameter that is used to specify the names of the ins
3 min read
How To Abort a Stash Pop in Git?
When working with Git, the stash feature is useful for saving changes that are not ready to be committed. Sometimes, while popping stashes, you might run into conflicts or simply change your mind. This article will walk you through how to abort a stash pop in Git. What is Git Stash?The Git stash command is a handy tool for temporarily preserving ch
3 min read
How To Undo Git Stash Pop That Results in Merge Conflict?
Using Git stash pop is a convenient way to reapply stashed changes to your working directory. However, if your working directory has evolved since you created the stash, the Git stash pop can result in merge conflicts. When this happens, you might find yourself needing to undo the stash pop operation to return to a stable state. Here's how you can
3 min read
How to create automatic pop-up using jQuery ?
In this article, we will be showing you how to make a custom automatic Pop-up box for subscribing to any service. You might have often come across a Pop-up box asking to subscribe to any services when you visit any particular site. Hence in this article, you will learn how to customize that Pop-up box and make your own. We will be using simple HTML
4 min read
How to Make A Pop-Up Window With Tailwind CSS?
A pop-up window is a small box that appears on your webpage, showing extra information or options without leaving the current page. A pop-up window for a website should be interactive, simple & good in UI. we will learn how to make a pop-up window with HTML & Tailwind CSS and simple JavaScript. PrerequisitesHTMLCSSThese are the approaches t
4 min read
How to extend an existing array with another array without creating a new array in JavaScript ?
JavaScript arrays are a powerful data structure that allows you to store and manipulate collections of data. Often, we may need to combine or extend two or more arrays into a single array. It's easy to create a new array by concatenating two or more arrays but if we want to extend an existing array without creating a new array. There are two easy w
2 min read
How to compare two JavaScript array objects using jQuery/JavaScript ?
In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects. These are the methods to compare two JavaScript array objects: Using jQuery not() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using Lodash _.isEqual() MethodApproach 1: Using jQuery
3 min read
How does Promise.all() method differs from Promise.allSettled() method in JavaScript ?
In this article, we will first understand in brief the Promise.all() as well as Promise.allSettled() methods and then we will try to visualize how they differ from each other through some theoretical as well as some coding examples. Both Promise.all() and Promise.allSettled() methods are the methods of a Promise object (which is further a JavaScrip
4 min read
How does Promise.any() method differs from Promise.race() method in JavaScript ?
In this article, we will first try to understand how we may declare or use Promise.any() and Promise.race() methods followed by some facts which will eventually help us to understand how they differ from each other (through theoretical as well as coding examples). Let us first quickly understand in brief about both the methods followed by their syn
5 min read
How toReversed() method is different from reverse() method in JavaScript
In JavaScript, reverse() and toReversed() methods do the same job as reversing but toReversed() does not change the original array. The reverse() method changes the original array. Below we have explained both methods. Table of Content Reverse() MethodtoReversed() methodReverse() MethodThe reverse() method is a built-in method for arrays in JavaScr
2 min read
Implement polyfill for Array.prototype.map() method in JavaScript
In this article, we will learn how to implement a polyfill for an Array.prototype.map() method in JavaScript. What is a polyfill? A polyfill is a piece of computer code written to implement a feature in a browser that does not yet support it. It could be because of the older version of the browser you are using, or because the new version of the br
3 min read