Removing Elements from an Array
Another command that makes working with arrays easier is the pop()
command, which removes the last element from an array and returns its value. Similar to the push()
command, it uses dot notation.
Let's look at an example:
const colors = ["Blue", "Green", "Pink"];
const lastColor = colors.pop();
console.log(lastColor); // Prints: Pink
console.log(colors); // Prints: Blue,Green
The command colors.pop()
removed the last element from the colors
array. The value of this element ("Pink"
) was stored in the lastColor
variable. Then we printed the lastColor
variable and the colors
array.
Instructions
Remove the last coworker from the coworkers
array using the pop()
command and store it in a variable named lastCoworker
.
Print the value of the lastCoworker
variable.
Print the value of the coworkers
variable.
Start programming for free
7/10