let myArray = [1, 2, 3, 4, 5];
let removedItem = myArray.pop();
In the code above, we have an array called myArray with five elements. When we call the pop() method on myArray, it removes the last element (5) and returns it. We also save that value in the variable removedItem for later use. Now, if we were to console.log() myArray, we would see that the last element has been removed:
console.log(myArray); // [1, 2, 3, 4]
console.log(removedItem); // 5
It’s important to note that the pop() method only removes the last element of an array. If you want to remove an element from somewhere else in the array, you’ll need to use a different method, like splice().