JavaScript Notes
Master JavaScript array shift() and unshift(): add/remove from start, queue pattern, return values, mutation, performance vs push/pop, real-world examples, and interview Q&A.
shift() and unshift() are the counterparts of pop() and push() — but they work at the beginning (front) of the array. Together, all four methods give you full control over both ends of an array.
shift() — Remove from the Front
Syntax
array.shift()
// Returns: the removed element (or undefined if empty)
// Mutates: YES
// Performance: O(n) — must re-index all remaining elementsBasic Examples
Removed: "first" Remaining: ["second", "third"]
Shift from Empty Array
const empty = [];
const result = empty.shift();
console.log(result); // undefined — no error!
console.log(empty.length); // 0undefined 0
Queue Data Structure with unshift() + shift()
A queue follows FIFO (First In, First Out) — like a ticket line. People join at the back (push) and leave from the front (shift):
Front: "Alice" Serve: "Alice" Serve: "Bob" Size: 1
Real World Use Cases
Printing: Document1.pdf Printing: Report.xlsx Printing: Photo.png "🚨 URGENT: Server down!" Recent: ["vue", "typescript", "node", "react", "python"]
All Four Array End Methods Compared
| Method | End | Action | Returns | Mutates | O(?) |
|---|---|---|---|---|---|
push() | Back | Add | New length | ✅ Yes | O(1) |
pop() | Back | Remove | Removed element | ✅ Yes | O(1) |
unshift() | Front | Add | New length | ✅ Yes | O(n) |
shift() | Front | Remove | Removed element | ✅ Yes | O(n) |
Why is shift/unshift O(n)? They must re-index every element after the operation. If you add to position 0, elements at index 0, 1, 2... must become 1, 2, 3... — that's O(n).
["A", "B", "C", "D"] ["A", "B", "C", "D", "E"] A ["B", "C", "D", "E"] E ["B", "C", "D"]
Memory Diagram: Why shift/unshift is O(n)
Common Mistakes
| ❌ Wrong | ✅ Correct | Why |
|---|---|---|
arr.unshift(item) in performance-critical loops | Use arr.push(item) then reverse | unshift is O(n) — in loops it becomes O(n²) |
arr.shift() without checking empty | if (arr.length) arr.shift() | Returns undefined on empty — can cause bugs |
arr.unshift(a, b) expecting [b, a, ...] | Gets [a, b, ...] | Multiple args are inserted in the order given |
Using unshift when push is sufficient | Check which end you need | Prefer push/pop (O(1)) over shift/unshift (O(n)) when order allows |
Cheat Sheet
Interview Questions
Q1. What does unshift() return?
The new length of the array after the element(s) were added.
Q2. What does shift() return?
The removed element from the front of the array. Returns undefined if the array is empty.
Q3. Do shift() and unshift() mutate the original array?
Yes, both mutate the original array. They re-index all existing elements.
Q4. What is the time complexity of shift() vs pop()?
shift() is O(n) — it must re-index every element after removing index 0. pop() is O(1) — it only removes the last element, no re-indexing needed.
Q5. How is a queue different from a stack?
A stack is LIFO (Last In, First Out) — use push()/pop(). A queue is FIFO (First In, First Out) — use push() to add and shift() to remove.
Q6. Why is unshift() slower than push() for large arrays?
push() just appends to the end in O(1) time. unshift() inserts at the front, forcing JavaScript to move every existing element one position to the right — O(n) work.
Q7. How would you add multiple elements to the front while preserving their order?
arr.unshift("a", "b", "c") inserts them in order — result starts with ["a", "b", "c", ...]. This is the same order you'd expect.
Key Takeaways
unshift()adds to the front, returns new length — O(n)shift()removes from the front, returns the removed element — O(n)- Both mutate the original array
shift/unshiftare slower thanpush/popdue to re-indexing- Together,
push()+shift()implement the FIFO queue pattern - For performance-sensitive code, prefer
push/pop(O(1)) overshift/unshift(O(n))
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Array shift() and unshift() - Complete Tutorial with Examples.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, arrays, shift, unshift
Related JavaScript Master Course Topics