JavaScript Roadmap — Day 32: Array Methods — Push, Pop, Shift, Unshift
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.
š” 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).
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.
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.
Question 3: Which methods create a Stack (LIFO) pattern?
5. Common Mistakes to Avoid
6. Best Practices — Professional Advice
arr.push(...otherArr) is a clean way to merge arrays without loops.7. Try It Yourself — Array Methods Playground
Experiment with push, pop, unshift, and shift below. See how each method changes the array.
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.
- 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()
❓ 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).