What does pop do in javascript?

AffiliatePal is reader-supported. When you buy through links on our site, we may earn an affiliate commission.



In JavaScript, the pop function is used to remove the last element from an array and return that element. This built-in method is commonly used to manipulate arrays by removing elements from the end. Understanding how pop works and its various applications can greatly enhance your JavaScript programming skills.

How does pop work?

The pop function is a method of the Array object in JavaScript. When called on an array, it removes the last element from the array and returns that element. The original array is modified, and its length is decreased by one.

Here’s an example to illustrate the usage of pop:

let fruits = [‘apple’, ‘banana’, ‘orange’];
let lastFruit = fruits.pop();
console.log(lastFruit); // Output: ‘orange’
console.log(fruits); // Output: [‘apple’, ‘banana’]

In this example, the pop function is called on the `fruits` array, and the last element, ‘orange’, is removed and assigned to the variable `lastFruit`. The modified array `fruits` now contains only ‘apple’ and ‘banana’.

Applications of pop

1. Removing and retrieving the last element: The primary use of pop is to remove the last element from an array and retrieve it. This can be useful when you need to process elements in reverse order or when you want to implement a stack-like behavior.

2. Implementing a stack: A stack is a data structure that follows the Last-In-First-Out (LIFO) principle. The pop function can be used to implement the pop operation in a stack. By repeatedly calling pop, you can remove elements from the top of the stack.

3. Dynamic resizing of arrays: When working with dynamically resizing arrays, the pop function is often used to remove elements from the end of the array when it becomes unnecessary. This helps in optimizing memory usage and improving performance.


In JavaScript, the pop function plays a crucial role in manipulating arrays by removing the last element. It is commonly used to implement stack operations, process elements in reverse order, and optimize memory usage. By understanding how pop works and its various applications, you can efficiently work with arrays in JavaScript.


– developer.mozilla.org
– www.w3schools.com
– stackoverflow.com