JavaScript Roadmap — Day 32: Array Methods — Push, Pop, Shift, Unshift

 

 

Day 32 of 180 šŸ• 12 min read · ⚡ Intermediate Phase

Array Methods — Push, Pop, Shift, Unshift

Learn how to add and remove elements from arrays using push, pop, shift, and unshift — the foundation of array manipulation.

1. The Problem — Adding and Removing Items

Once you have an array, you'll often need to add new items or remove existing ones. JavaScript provides four fundamental methods for this: push(), pop(), unshift(), and shift(). These methods let you manipulate arrays efficiently.

Array Manipulation
// Starting array
let fruits = ["apple", "banana"];

// Add to END
fruits.push("cherry");     // ["apple", "banana", "cherry"]

// Remove from END
fruits.pop();            // ["apple", "banana"] (returns "cherry")

// Add to BEGINNING
fruits.unshift("mango");   // ["mango", "apple", "banana"]

// Remove from BEGINNING
fruits.shift();          // ["apple", "banana"] (returns "mango")

šŸ’” The insight: push() and pop() work on the end of the array (fast). unshift() and shift() work on the beginning (slower because all elements need to reindex).

2. push() and pop() — Working with the End

push() adds one or more elements to the end of an array and returns the new length. pop() removes the last element and returns it. These are the most commonly used array methods because they are fast (O(1) operation).

push() and pop() Examples
let stack = [];

// push() — add to end
stack.push("first");
stack.push("second");
stack.push("third");
console.log(stack);  // ["first", "second", "third"]

// pop() — remove from end
let last = stack.pop();
console.log(last);     // "third"
console.log(stack);    // ["first", "second"]

// push() can add multiple items at once
stack.push("fourth", "fifth");
console.log(stack);  // ["first", "second", "fourth", "fifth"]
šŸ“ QUIZ Test Your Understanding

Question 1: What does pop() return?

3. unshift() and shift() — Working with the Beginning

unshift() adds elements to the beginning of an array. shift() removes the first element. These operations are slower than push/pop because all other elements need to be re-indexed.

unshift() and shift() Examples
let queue = ["second", "third"];

// unshift() — add to beginning
queue.unshift("first");
console.log(queue);  // ["first", "second", "third"]

// shift() — remove from beginning
let first = queue.shift();
console.log(first);    // "first"
console.log(queue);    // ["second", "third"]

// unshift() can add multiple items
queue.unshift("zero", "one");
console.log(queue);  // ["zero", "one", "second", "third"]
šŸ“ QUIZ Test Your Understanding

Question 2: Which method adds an element to the BEGINNING of an array?

4. Stack vs Queue — Real-World Patterns

These four methods enable two important data structures: Stacks (LIFO — Last In, First Out) using push/pop, and Queues (FIFO — First In, First Out) using push/shift.

Stack and Queue
// STACK (LIFO) — Last In, First Out
let stack = [];
stack.push("first");
stack.push("second");
stack.push("third");
stack.pop();  // removes "third" (last in)
console.log(stack);  // ["first", "second"]

// QUEUE (FIFO) — First In, First Out
let queue = [];
queue.push("first");
queue.push("second");
queue.push("third");
queue.shift();  // removes "first" (first in)
console.log(queue);  // ["second", "third"]
šŸ“ QUIZ Test Your Understanding

Question 3: Which methods create a Stack (LIFO) pattern?

5. Common Mistakes to Avoid

Mistakes & Fixes
// ❌ MISTAKE 1: Forgetting that pop() returns the removed element
let arr = [1, 2, 3];
arr.pop();           // removes 3 but you didn't capture it
console.log(arr);    // [1, 2]

// ✅ FIX: Capture the returned value if you need it
let removed = arr.pop();
console.log(removed);  // 3

// ❌ MISTAKE 2: Confusing push() return value
let len = arr.push(4);
console.log(len);  // returns NEW length, not the array

// ✅ FIX: Remember push() returns length, not the array
arr.push(4);
console.log(arr);  // [1, 2, 4]

// ❌ MISTAKE 3: Using shift() on empty array
let empty = [];
let result = empty.shift();
console.log(result);  // undefined (not an error)

6. Best Practices — Professional Advice

šŸŽÆ Prefer push() and pop() for Performance
push() and pop() are O(1) operations (very fast). unshift() and shift() are O(n) (slower for large arrays). Use push/pop when possible.
šŸ“¦ Use push() with Spread Operator for Merging
arr.push(...otherArr) is a clean way to merge arrays without loops.
šŸ”„ Choose the Right Data Structure
Use Stack (push/pop) for undo/redo functionality. Use Queue (push/shift) for task processing.

7. Try It Yourself — Array Methods Playground

Experiment with push, pop, unshift, and shift below. See how each method changes the array.

✏️ Array Methods Playground
HTML
JAVASCRIPT
LIVE PREVIEW
šŸ’” Try push(), pop(), unshift(), and shift()!

8. Practice Tasks

Task 1 — Easy: Shopping Cart

Create an empty array called cart. Use push() to add "Apple", "Banana", and "Cherry". Then use pop() to remove the last item. Log the final cart.

Task 2 — Medium: Queue Simulator

Create a queue using push() to add tasks: "Task 1", "Task 2", "Task 3". Then use shift() to process each task one by one. Log each task as it's processed.

Task 3 — Hard: Undo/Redo System

Create a simple undo/redo system using two stacks (push/pop). When user "adds" an action, push to history. When undo is called, pop from history and push to redo stack.

šŸŽÆ Key Takeaways
  • push() — adds to end, returns new length
  • pop() — removes from end, returns removed element
  • unshift() — adds to beginning, returns new length (slower)
  • shift() — removes from beginning, returns removed element (slower)
  • Stack (LIFO) — push() + pop()
  • Queue (FIFO) — push() + shift()
šŸ’¼ Common Interview Questions

Q: What's the difference between push() and unshift()?

šŸ’” A: push() adds to the end, unshift() adds to the beginning.

Q: Which is faster: pop() or shift()? Why?

šŸ’” A: pop() is faster (O(1)) because it doesn't need to reindex. shift() is slower (O(n)) because all elements must move.

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

šŸ’” A: undefined (not an error).

šŸ“– Download Array Methods Cheat Sheet
Quick reference for push, pop, unshift, shift
Next Lesson
Day 33 — More Array Methods (slice, splice)
View Full Roadmap →
Enjoying this roadmap?
Follow Muhammad Waheed Asghar for daily JavaScript tips and updates!

Popular Posts