push() and pop(): Adding and Removing from the End
You'll learn how to add elements to the end of an array with push() and remove the last element with pop().
The Core Idea
Arrays in JavaScript come with built-in methods that make it easy to work with the "end" of the array. Think of an array like a stack of plates:
push()adds one or more elements to the end of the array (placing a new plate on top of the stack)pop()removes and returns the last element (taking the top plate off)
Both methods mutate the original array — they change it directly rather than creating a new array.
Using push()
The push() method adds elements to the end and returns the new length of the array:
const fruits = ['apple', 'banana'];
const newLength = fruits.push('cherry');
console.log(fruits); // ['apple', 'banana', 'cherry']
console.log(newLength); // 3
You can push multiple elements at once:
fruits.push('date', 'elderberry');
console.log(fruits); // ['apple', 'banana', 'cherry', 'date', 'elderberry']
Using pop()
The pop() method removes the last element and returns it:
const lastFruit = fruits.pop();
console.log(lastFruit); // 'elderberry'
console.log(fruits); // ['apple', 'banana', 'cherry', 'date']
If you call pop() on an empty array, it returns undefined:
const empty = [];
console.log(empty.pop()); // undefined
Key Takeaway: push() adds elements to the end of an array and returns the new length, while pop() removes and returns the last element — both methods mutate the original array.