JS Array Push and Pop Methods: A Complete Guide

Learn how push() and pop() add and remove elements from the end of a JavaScript array, what they return, and when to use each one.

6 min read

push() and pop() are the two array methods that add and remove elements from the end of an array. Together they let you treat an array like a stack -- you add to the top and remove from the top.

push and pop operate on the array end

push() adds to the end and returns the new length. pop() removes from the end and returns the element it removed.

push() -- Add to the End

push() takes one or more arguments and adds them to the end of the array. It mutates the original array and returns the new length.

javascriptjavascript
const fruits = ["apple", "banana"];
 
const newLength = fruits.push("orange");
 
console.log(fruits);    // ["apple", "banana", "orange"]
console.log(newLength); // 3

The return value is the new length, not the added element. This is a common point of confusion.

You can push multiple elements at once:

javascriptjavascript
const numbers = [1, 2];
 
numbers.push(3, 4, 5);
 
console.log(numbers); // [1, 2, 3, 4, 5]

The arguments are added in the order you pass them: 3 first, then 4, then 5.

Practical push() Use Cases

Building an array from a loop:

javascriptjavascript
const evens = [];
 
for (let i = 0; i <= 10; i++) {
  if (i % 2 === 0) {
    evens.push(i);
  }
}
 
console.log(evens); // [0, 2, 4, 6, 8, 10]

Collecting user input works the same way. Each call to push() adds one task and hands back the new count, which you can show to the user without a separate length lookup:

javascriptjavascript
const todoList = [];
 
function addTask(task) {
  const length = todoList.push(task);
  console.log(`Task added. ${length} total tasks.`);
}
 
addTask("Buy groceries"); // Task added. 1 total tasks.
addTask("Walk the dog");   // Task added. 2 total tasks.

pop() -- Remove from the End

pop() removes the last element from an array and returns it. It mutates the original array.

javascriptjavascript
const colors = ["red", "green", "blue"];
 
const removed = colors.pop();
 
console.log(colors);  // ["red", "green"]
console.log(removed); // "blue"

The array shrinks by one. The removed value is returned so you can use it.

If you call pop() on an empty array, it returns undefined:

javascriptjavascript
const empty = [];
 
console.log(empty.pop()); // undefined
console.log(empty);       // [] (still empty, no error)

pop() never throws an error, even on an empty array.

Practical pop() Use Cases

Processing items in reverse order is a common use for pop(). Looping while the array still has items and popping one each time visits them from the last pushed to the first:

javascriptjavascript
const stack = ["step1", "step2", "step3"];
 
while (stack.length > 0) {
  const step = stack.pop();
  console.log(`Processing: ${step}`);
}
// Processing: step3
// Processing: step2
// Processing: step1

This is a LIFO (last in, first out) pattern. The last item pushed is the first one popped. It is useful for undo operations, back-button navigation, and expression evaluation.

Getting and removing the most recent item:

javascriptjavascript
const recentSearches = ["pizza", "sushi", "tacos"];
 
const lastSearch = recentSearches.pop();
console.log(lastSearch); // "tacos"

push() + pop() Together: The Stack Pattern

A stack is a data structure where you only interact with the end. You push to add and pop to remove. A browser history is a good example: each page visit pushes a new URL onto the end of the array.

javascriptjavascript
const history = [];
 
history.push("/home");
history.push("/products");
history.push("/products/42");
 
console.log(history); // ["/home", "/products", "/products/42"]

Clicking the back button pops the most recent URL off the end and returns to the page the user was on before that, which is exactly how pop() behaves on any array used as a stack:

javascriptjavascript
const history = ["/home", "/products", "/products/42"];
 
const previous = history.pop();
console.log(previous); // "/products/42"
console.log(history);  // ["/home", "/products"]

This is the foundation of browser back-button behavior and undo systems.

Comparison: push/pop vs shift/unshift

MethodOperates OnMutates?ReturnsPerformance
push()End of arrayYesNew lengthFast (O(1))
pop()End of arrayYesRemoved elementFast (O(1))
shift()Start of arrayYesRemoved elementSlower (O(n))
unshift()Start of arrayYesNew lengthSlower (O(n))

push() and pop() are fast because they only touch the end of the array. shift() and unshift() are slower on large arrays because every existing element must be re-indexed.

Common Mistakes

Expecting push() to return the element:

javascriptjavascript
const arr = [1, 2];
 
const element = arr.push(3);
console.log(element); // 3 (the new length, not the value 3)

It is easy to misread that 3 as the pushed value, but it is really the array's new length, which only looks like the pushed value here by coincidence. Always remember that push() returns the new length, and the array itself is already updated by the time it returns.

Using pop() when you only need to read the last element:

javascriptjavascript
const arr = ["a", "b", "c"];
 
// Wrong: removes the element when you just wanted to see it
const last = arr.pop();
console.log(arr); // ["a", "b"] -- permanently changed!
 
// Right: read without removing
const lastRead = arr[arr.length - 1]; // "b"

Use bracket notation or at(-1) when you only need to read, not remove. For more on reading elements safely, see Accessing and Modifying JS Array Elements Guide.

Now that you can add and remove from the end, learn how to do the same from the start with shift and unshift.

Rune AI

Rune AI

Key Insights

  • push() adds one or more elements to the end of an array and returns the new length.
  • pop() removes the last element from an array and returns that removed element.
  • Both methods mutate the original array. They do not create a copy.
  • push() and pop() together make arrays behave like stacks (LIFO).
  • pop() on an empty array returns undefined without throwing an error.
RunePowered by Rune AI

Frequently Asked Questions

Does push() return the added element?

No. push() returns the new length of the array after adding the element, not the element itself. The array is mutated in place.

What does pop() return if the array is empty?

pop() returns undefined when called on an empty array. It does not throw an error.

Can push() add multiple elements at once?

Yes. You can pass multiple arguments to push(), and all of them will be added to the end of the array in the order you pass them.

Conclusion

push() and pop() are the simplest way to treat an array like a stack. push() adds to the end and returns the new length. pop() removes from the end and returns the removed element. Together they give you a clean LIFO (last in, first out) data structure with minimal code. When you need to add or remove from the beginning of an array instead, use shift() and unshift().