Blog Post
Understanding the pop() Method in JavaScript ❌
The pop() method removes the last element of an array and returns that element. This operation also adjusts the length of the array. If the array is empty, pop() returns undefined.
👩💻 Try this example in the console: Press F12 to open DevTools, navigate to the ‘console’ tab, and paste the code provided below. Press Enter and experiment with the code!
Code
const array1 = [1, 2, 3, 4, 5];
const popArray = array1.pop();
console.log(popArray);
// output: 5
console.log(array1);
// output: [1, 2, 3, 4]As shown above, pop() removes the last element (5) from the array and returns it, leaving the array with the remaining elements.