Understanding the pop() Method in JavaScript ❌

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!

<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