javascript / beginner
Snippet
Removing the Last Item with Array.pop()
The 'pop' method removes the last element from an array. It also returns that element so you can store it in a variable if needed.
snippet.js
1
2
3
const items = ["Milk", "Bread", "Eggs"];const last = items.pop();console.log(items);
nodejs
Breakdown
1
const items = ["Milk", "Bread", "Eggs"];
Initializes an array with three string elements.
2
const last = items.pop();
Removes 'Eggs' from the array and assigns it to the variable 'last'.
3
console.log(items);
Prints the updated array, which now only contains two items.