JavaScript Array pop() method removes the last element from an input array and returns the removed element.
It modifies the original array in place by reducing its length property by 1.
let countries = ["UK", "US", "India", "Israel", "Mexico"]; console.log('length of countries array before popping: ', countries.length); // Output: length of countries array before popping: 5 let removedItem = countries.pop(); console.log('The remaining items are: ', countries); // Output: The remaining items are: [ 'UK', 'US', 'India', 'Israel' ] console.log('The removed item is: ', removedItem); // Output: The removed item is: Mexico console.log('length of countries array: ', countries.length); // Output: length of countries array: 4
Syntax
array.pop()
Parameters
It does not take any argument.
Empty array
If you try to pop an element from an empty array, it returns undefined.
let countries = []; let removedItem = countries.pop(); console.log(removedItem); // Output: undefined console.log(countries.length); // Output: 0
Array with a single element
If the input array contains a single element and you remove that, the array becomes empty.
let single = [42]; let removed = single.pop(); console.log(removed); // Output: 42 console.log(single); // Output: [] console.log(single.length); // Output: 0
Removing an element from an array of objects
Let’s say you have an array filled with different objects. You can remove the last object from an array using the pop() method. No matter whether the element is a number, string, or object, the pop() works the same.
let apps = [ { name: 'Amazon', value: 1 }, { name: 'Amazon Prime', value: 21 }, { name: 'Amazon Music', value: 10 }, { name: 'Amazon Go', value: 100 }, { name: 'Amazon Alexa', value: 13 } ]; removedItem = apps.pop(); console.log('The remaining items are: ', apps); // Output: The remaining items are: // [ // { name: 'Amazon', value: 1 }, // { name: 'Amazon Prime', value: 21 }, // { name: 'Amazon Music', value: 10 }, // { name: 'Amazon Go', value: 100 } // ] console.log('The removed item is: ', removedItem); // Output: The removed item is: { name: 'Amazon Alexa', value: 13 }
As you can see from the output, the removed element is the last object.
Clearing the whole array
You can clear the whole array using a for loop and the pop() method. Just iterate through each element and remove the item one by one, unless the entire array becomes empty.
let stack_array = [11, 19, 3, 21]; while (stack_array.length > 0) { stack_array.pop(); } console.log("Final Stack:", stack_array); // Output: Final Stack: []
Sparse arrays
A sparse array is an array with holes in it. Holes, meaning empty slots. An empty slot counts as an element in the array.
const sparse_array = ['k', , 'b', , 'l']; // Holes at index 1 and 3 console.log(sparse_array.length); // Output: 5 sparse_array.pop(); // Removes 'l' console.log(sparse_array); // Output: [ 'k', <1 empty item>, 'b', <1 empty item> ] console.log(sparse_array.length); // Output: 4
The sparse array’s length is 5, apparently because empty slots are counted. After the removal of the last element, the updated length is 4.