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!
<pre class="preFormatted"><code class="codeFormatted">
const array1 = [1, 2, 3, 4, 5];
const popArray = array1.pop();
console.log(popArray);
// output: 5
console.log(array1);
// output: [1, 2, 3, 4]
</code></pre>
As shown above, `pop()` removes the last element (5) from the array and returns it, leaving the array with the remaining elements.
Published: April 2024
β Back to Blog