Removing Elements: pop()
The opposite of push() is the method pop(): it removes the last element of an array. You don't write anything in the parentheses, because it always removes the last element. But you do have to write the parentheses.
Besides changing the array, pop() gives the removed element back to you, so that you can keep working with it. We say that the method returns the element, and the element is its return value. You can store the returned value in a variable:
const colors = ["blue", "green", "pink"];
const lastColor = colors.pop();
console.log(lastColor);
console.log(colors);This prints:
pink
["blue", "green"]The call colors.pop() removed the element "pink" from the array and returned it, so it was stored in the variable lastColor. If you don't need the removed element, simply write colors.pop(); on a line of its own.
Don't forget the parentheses. Without them, the method isn't called: const lastColor = colors.pop; removes nothing and stores the method itself in lastColor. When you print it, you see [Function: pop] instead of an element.
Instructions
Ava's project is finished, and she is leaving the team. Remove the last element of coworkers with pop() and store it in a constant leavingCoworker.
Print leavingCoworker.
Print coworkers to check that Ava is no longer in the array.