Last time, we learned how to add items to the end of an array using .push()
, which lets our arrays grow.
Engagement Message
But what if we need to take an item out of an array? For example, removing the last task you added.
JavaScript arrays have a simple method that is the perfect opposite of .push()
: the .pop()
method.
This method removes the item from the very end of the array. The array gets shorter by one item.
Engagement Message
Does .pop()
seem like a useful tool for an "undo" feature?
The syntax is simple: arrayName.pop();
. It doesn't need any arguments inside the parentheses because it always knows to remove the last item.
Engagement Message
If we have ["milk", "eggs", "bread"];
and try using pop()
, how many elements do you think will remain?
After shopping.pop();
, the array becomes ["milk", "eggs"]
. The last item, "bread"
, is gone.
