Hello again!
In our last lesson, we took our first steps with arrays, learning how to create them and how to access or change their elements using index numbers. This gave us a way to manage ordered lists of data.
Today, we'll build on that foundation. While accessing elements by index is powerful, a common need is to dynamically grow or shrink an array by adding or removing items. This lesson focuses on four fundamental methods that let you do just that: push, pop, shift, and unshift. These methods are your primary tools for adding and removing elements from the ends of an array.
By the end of this lesson, you will be able to confidently modify arrays by adding items to the beginning or end, and removing them from the beginning or end.
Adding and Removing from the End: push() and pop()
Let's start with the most common and efficient way to modify an array: changing its end. The methods for this are push() and pop().
push(item): Adds one or more elements to the end of an array.pop(): Removes the last element from an array.
Imagine you have a list of tasks to do. You can push a new task onto the end of the list. When you've completed the last task you added, you can pop it off.
let tasks = ['Code', 'Eat', 'Sleep'];
console.log(tasks); // Output: ['Code', 'Eat', 'Sleep']
// Let's add a new task to the end
tasks.push('Repeat');
console.log(tasks); // Output: ['Code', 'Eat', 'Sleep', 'Repeat']
// Now, let's remove the last task
let removedTask = tasks.pop();
console.log(removedTask); // Output: 'Repeat'
console.log(tasks); // Output: ['Code', 'Eat', 'Sleep']
Notice two important details:
push()modifies the original array and returns the new length of the array.pop()also modifies the original array, but it returns the element that was removed.
The "Why": Stack Behavior and Efficiency
Your preference for understanding the "why" behind concepts is very useful here. The push and pop methods are named after a classic computer science data structure called a stack.
The article below provides a fantastic analogy and explains the underlying reason for the efficiency of these methods.
JavaScript Array Essentials: Using pop, push, shift, and ...
Read this section from the article 'JavaScript Array Essentials' to understand the stack data structure analogy and why push and pop are so efficient.
Read the section 'The .push() and .pop() JavaScript Array Methods'. Pay close attention to the 'stack of plates' analogy, the LIFO (Last-In, First-Out) principle, and the mention of O(1) time complexity, which means the operation is very fast regardless of the array's size.
As the article explains, a stack follows a "Last-In, First-Out" (LIFO) principle. The last item you add (push) is the first item you remove (pop). Because these operations happen at the end of the array, JavaScript doesn't need to re-calculate the index for every other element, making them extremely fast.
This visual guide neatly illustrates the push and pop operations.

Test your understanding!
You are managing a shopping cart represented by an array.
let cart = ['Apples', 'Bread'];
- Add 'Milk' to the end of the cart.
- Now, the customer changes their mind and wants to remove the last item they added. Remove it.
- Log the final
cartarray to the console. What is the expected output?
Show answer
let cart = ['Apples', 'Bread'];
// 1. Add 'Milk'
cart.push('Milk'); // cart is now ['Apples', 'Bread', 'Milk']
// 2. Remove the last item
cart.pop(); // cart is now ['Apples', 'Bread']
// 3. Log the final array
console.log(cart); // Expected output: ['Apples', 'Bread']
Adding and Removing from the Beginning: unshift() and shift()
What if you need to add or remove items from the start of an array? For this, JavaScript provides unshift() and shift().
unshift(item): Adds one or more elements to the beginning of an array.shift(): Removes the first element from an array.
Let's imagine a queue of people waiting. A new person (unshift) goes to the front of the line, and the next person to be served (shift) is also at the front.
let line = ['Bob', 'Charlie'];
console.log(line); // Output: ['Bob', 'Charlie']
// Alice cuts in at the beginning of the line
line.unshift('Alice');
console.log(line); // Output: ['Alice', 'Bob', 'Charlie']
// It's Alice's turn, so she is removed from the front
let serving = line.shift();
console.log(serving); // Output: 'Alice'
console.log(line); // Output: ['Bob', 'Charlie']
Similar to push and pop:
unshift()modifies the array and returns its new length.shift()modifies the array and returns the element that was removed.
This visual guide shows shift and unshift in action.

The "Why": Performance Cost of Shifting
Unlike push and pop, the shift and unshift methods are less efficient, especially for large arrays. Why is that? When you add or remove an element at the beginning, the index of every other element in the array must be changed.
The same article from CodingNomads explains this concept perfectly.
JavaScript Array Essentials: Using pop, push, shift, and ...
Now, let's explore why shift and unshift have a performance cost. This section explains the concept of re-indexing.
Read the section 'The .shift() and .unshift() JavaScript Array Methods'. Focus on the explanation of why these methods are less efficient and have O(n) time complexity. The idea of elements being in 'contiguous memory locations' is the key here.
This "under the hood" knowledge is what separates a novice from an expert. While you shouldn't worry too much about this for small arrays, it's a critical concept to understand for building high-performance applications later, especially with large datasets in Next.js.
A Quick Video Summary
To see all four methods in action and hear them explained, the following video provides a great, quick summary.
JavaScript Fundamentals: Using push, pop, unshift and shift to Manipulate Arrays
The video 'JavaScript Fundamentals: Using push, pop, unshift and shift...' from All Things JavaScript, LLC provides a concise explanation and demonstration of all four methods.
Watch the segment from 01:19 to 02:29. This part gives you a clear, side-by-side definition of what each method does and what it returns.
Pushing and Unshifting Multiple Items
A final useful feature is that both push() and unshift() can accept multiple arguments at once, allowing you to add several elements in a single, readable line of code.
let numbers = [10, 20, 30];
// Add multiple items to the end
numbers.push(40, 50);
console.log(numbers); // Output: [10, 20, 30, 40, 50]
// Add multiple items to the beginning
numbers.unshift(0, 5);
console.log(numbers); // Output: [0, 5, 10, 20, 30, 40, 50]
Test your understanding!
You are managing a playlist. Start with this array:let playlist = ['Song C', 'Song D'];
Perform these operations in order:
- Add 'Song A' and 'Song B' to the beginning of the playlist in one command.
- Add 'Song E' to the end of the playlist.
- The user skips the first song. Remove it.
- Log the final
playlistto the console. What is the expected output?
Show answer
let playlist = ['Song C', 'Song D'];
// 1. Add to beginning
playlist.unshift('Song A', 'Song B');
// playlist is now ['Song A', 'Song B', 'Song C', 'Song D']
// 2. Add to end
playlist.push('Song E');
// playlist is now ['Song A', 'Song B', 'Song C', 'Song D', 'Song E']
// 3. Remove from beginning
playlist.shift();
// playlist is now ['Song B', 'Song C', 'Song D', 'Song E']
// 4. Log the final array
console.log(playlist);
// Expected output: ['Song B', 'Song C', 'Song D', 'Song E']
Conclusion
You've now learned the four fundamental methods for adding and removing elements from the ends of a JavaScript array. Mastering these is crucial for almost any program you will write.
Key Takeaways:
push(): Adds to the end. Fast (LIFO stack behavior).pop(): Removes from the end. Fast.unshift(): Adds to the beginning. Can be slow because it re-indexes the array.shift(): Removes from the beginning. Can be slow for the same reason.- Methods for removing elements (
pop,shift) return the removed element. - Methods for adding elements (
push,unshift) return the new array length.
Next Up
These four methods are perfect for managing the ends of an array. But what if you need to add or remove an element from the middle of an array? In the next lesson, we will explore slice() and splice(), powerful methods that give you precise control over any part of an array.