Array push() and pop()
Quick answer
Use push to append values and pop to remove the final value. Both methods mutate the receiver array; their return values are different.
How it works
items.push(value1, value2) returns the resulting length. items.pop() returns the removed value, or undefined for an empty array. A const array binding permits this mutation because the array identity is unchanged.
const a = [2];console.log(a.push(4, 6), a.length);console.log(a.pop(), a.length);Expected output in Node.js:
3 36 2Appending two values produces length three; pop returns 6.
const history = [];history.push("home");history.push("detail");console.log(history.pop(), history[0]);Expected output in Node.js:
detail homeThe most recently appended value is removed first.
Common mistakes
Do not assign a = a.push(x) expecting an array; push returns a number. Check empty arrays before relying on a popped value.
Practice
Create a stack of three tasks, then pop twice and inspect the remaining item.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Array push() and pop() — Knowledge Check
Apply the taught concept and reason about the result.
What does [1,2].push(3) return?
What does [1,2].pop() return?
What does [].pop() return?
Can const a=[]; a.push(1); change the array?
After const a=[1]; a.push(2,3); a.pop(); what is a.length?