JavaScript Notes
Master JavaScript array push() and pop() methods: add/remove from end, stack pattern, return values, mutation, performance, vs other methods, and interview Q&A.
push() and pop() are the two simplest array methods — and among the most used. They work on the end of an array and are the building blocks of the stack data structure.
pop() — Remove from the End
Syntax
array.pop()
// Returns: the removed element (or undefined if empty)
// Mutates: YESBasic Examples
Removed: "Cherry" Remaining: ["Apple", "Banana"]
Pop from Empty Array
const empty = [];
const result = empty.pop();
console.log(result); // undefined — no error thrown!
console.log(empty.length); // 0undefined 0
Stack Data Structure with push() + pop()
A stack follows LIFO (Last In, First Out) — like a stack of plates. push() and pop() implement this perfectly:
Top: "JavaScript" Pop: "JavaScript" Pop: "CSS" Size: 1
Real World Use Cases
Done: Draw circle Done: Fill red Done: Add text Undone: Add text Undone: Fill red Stack: ["main()", "fetchData()", "parseJSON()"] Stack: ["main()"] Even × 10: [20, 40]
push() and pop() vs Other Methods
| Method | End/Start | Action | Returns | Mutates |
|---|---|---|---|---|
push() | End | Add | New length | ✅ Yes |
pop() | End | Remove | Removed element | ✅ Yes |
unshift() | Start | Add | New length | ✅ Yes |
shift() | Start | Remove | Removed element | ✅ Yes |
concat() | End | Add (copy) | New array | ❌ No |
splice() | Any | Add/Remove | Removed elements | ✅ Yes |
["A", "B", "C"] ["A", "B", "C", "D"] A ["B", "C", "D"] D ["B", "C"]
Common Mistakes
| ❌ Wrong | ✅ Correct | Why |
|---|---|---|
arr.push([1, 2, 3]) to merge | arr.push(...[1, 2, 3]) | Push without spread creates nested array |
arr.pop(item) — passing argument | arr.pop() | pop() takes no arguments — extra args are ignored |
arr.push() ignoring return value | const len = arr.push(item) | Return value is the new length — useful to know |
Calling pop() without checking empty | if (arr.length) arr.pop() or use optional: arr.at(-1) check | pop() on empty returns undefined — can cause bugs if used as object |
Cheat Sheet
Interview Questions
Q1. What does push() return?
The new length of the array after the element(s) were added.
Q2. What does pop() return?
The removed element from the end. If the array is empty, returns undefined.
Q3. Do push() and pop() mutate the original array?
Yes, both mutate the original array directly. They are in-place operations.
Q4. How do you add all elements of one array into another using push()?
Use the spread operator: arr1.push(...arr2). Without spread, the entire second array is added as a single nested element.
Q5. What is the difference between push() and concat()?
push() mutates the original array and adds elements directly. concat() returns a new array and does not modify the original.
Q6. How would you implement a stack in JavaScript?
Use an array with push() to add (PUSH) and pop() to remove (POP). The last item pushed is always the first item popped — LIFO behavior.
Q7. What is the time complexity of push() and pop()?
Both are O(1) (constant time) — they only operate on the last element without re-indexing the array.
Key Takeaways
push()adds to the end, returns new lengthpop()removes from the end, returns the removed element- Both mutate the original array
pop()on an empty array returnsundefined— no error thrownpush(...otherArr)spreads elements;push(otherArr)creates a nested array- Together,
push()+pop()implement the LIFO stack pattern - Time complexity: O(1) — fastest array operations
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for JavaScript Array push() and pop() - 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, push, pop
Related JavaScript Master Course Topics