This content originally appeared on Go Make Things and was authored by Go Make Things
Yesterday, we looked at the Array.shift()
method. Today, I wanted to talk about the Array.pop()
method. This is another quick one.
The Array.pop()
method removes the last item from an array and returns it. The array is modified.
let wizards = ['Gandalf', 'Radagast', 'Merlin'];
let last = wizards.pop();
// logs "Merlin"
console.log(last);
// logs ["Gandalf", "Radagast"]
console.log(wizards);
If you only need to get the last item in the array, you’re probably better off using bracket notation, like this.
let last = wizards[wizards.length - 1];
The Array.pop()
method is most useful when you want to actually remove the last item from the original array and modify its length.
This content originally appeared on Go Make Things and was authored by Go Make Things

Go Make Things | Sciencx (2021-03-03T15:30:00+00:00) The Array.pop() method in vanilla JS. Retrieved from https://www.scien.cx/2021/03/03/the-array-pop-method-in-vanilla-js/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.