What pop does
The pop method removes the last element of an array and returns it. The array gets one element shorter, and the removed value is handed back to you, so you can store it or ignore it.
const threeArr: number[] = [1, 4, 6];
const oneDown = threeArr.pop();
console.log(oneDown); // 6
console.log(threeArr); // [ 1, 4 ]
const twoDown = threeArr.pop();
console.log(twoDown); // 4
console.log(threeArr); // [ 1 ]
Each call looks at whatever is last at that moment. The first call removes 6, which leaves 4 at the end, so the second call removes 4. Each result gets its own name here (oneDown, twoDown): declaring the same name twice with let or const in one scope is an error, so use a new name or assign to a variable declared once.
pop and push
pop is the opposite of push. Together they make an array behave like a stack: you add at the end with push, and take back the most recently added item with pop. Both change the array in place, and both are cheap, because they only touch the last position.
const stack: string[] = [];
stack.push("a");
stack.push("b");
console.log(stack.pop()); // b
console.log(stack.pop()); // a
console.log(stack); // []
Popping an empty array
Calling pop on an empty array is not an error. It returns undefined and leaves the array empty:
const empty: number[] = [];
const value = empty.pop();
console.log(value); // undefined
console.log(empty); // []
This is easy to overlook, because the compiler treats the result of pop on a number[] as a plain number here, even though undefined can come back at run time. The types are erased when the code is compiled, so they won't warn you. When the array might be empty, check arr.length before you pop, or test the returned value against undefined.
Using the returned value
The return value is what makes pop more than a delete. You can process each item as you take it off, until the array is empty:
const tasks: string[] = ["wash", "cook", "eat"];
while (tasks.length > 0) {
const task = tasks.pop();
console.log(task);
}
// eat, cook, wash (one per line)
console.log(tasks.length); // 0
The items come out in reverse order, because the last one in is the first one out. The loop condition checks tasks.length, so pop is never called on an empty array.
The lesson's exercise
The starting code has two arrays: fruits with "mango", "kiwi" and "grapefruit", and ages with 14, 32, 50 and 21. The two steps are:
- Drop the last fruit. Call
fruits.pop(). That leaves["mango", "kiwi"]. The removed"grapefruit"is returned, but the exercise doesn't need it, so the call stands alone as a statement. - Drop the last two ages. Call
ages.pop()twice. The first call removes21and the second removes50, because after the first call50is the last element. That leaves[14, 32].
let fruits: string[] = ["mango", "kiwi", "grapefruit"];
let ages: number[] = [14, 32, 50, 21];
fruits.pop();
ages.pop();
ages.pop();
Counting the calls is the whole job: one pop removes one element, so removing two ages takes two calls. A single call never removes more than one element.
Gotchas
poptakes no argument. You can't tell it which item to remove.arr.pop(1)still removes only the last element, and the compiler reports the extra argument. To remove from other positions, usesplice.- It changes the original array. Every variable that refers to the same array sees the shorter array. Copy first with
[...arr]if you need the original too. - Don't assign the result back to the array.
fruits = fruits.pop()would replace the array with a single string. Callfruits.pop()and usefruitsas before. - Pop returns a value, not a new array. To get the array without its last item without changing it, use
arr.slice(0, -1). - The first item is a different method.
shiftremoves and returns the first element, but it has to move every remaining element one place to the left, so it does more work thanpop.
Key takeaways
arr.pop()removes the last element and returns it.- The array shrinks by one and is changed in place.
- On an empty array it returns
undefinedand does nothing else. - Use one call per element you want to remove.