JavaScript Glossary: Array .pop() Method
Publikováno: 8.2.2019
Basics
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.
Celý článekBasics
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()
https://scotch.io/embed/gist/1278ee97ff304b37ba2a9f7f530b38d3
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.
Syntax
array.pop()
No Parameters
Returns a string
The pop()
method will return the element removed from the array. If the array is empty, it returns undefined
.
const names = ['Johnny', 'Pete', 'Sammy']
console.log(names.pop());
// output: 'Sammy'
Common Uses and Snippets
Removing the last element of an array
The pop
method is mostly used to remove the last element of an array and output both the parent array and the value removed.
https://scotch.io/embed/gist/b75e39c247ac13bdb5ff6479cb1c66e9