Table of Contents
This method takes out the last element from an array and returns it. The
pop method mutates the array during this process reducing its length.
['me', 'him', 'you'].pop()
This method when called:
- Removes the element at the last index of the array.
- Mutates the parent array reducing the length.
- Returns the last element.
Returns a string
pop() method will return the element removed from the array. If the array is empty, it returns
const names = ['Johnny', 'Pete', 'Sammy'] console.log(names.pop()); // output: 'Sammy'
Removing the last element of an array
pop method is mostly used to remove the last element of an array and output both the parent array and the value removed.
Like this article? Follow @codebeast on Twitter