Open In App

What is the pop() method used for in JavaScript Arrays ?

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The pop() method in JavaScript is used to remove the last element from an array. It alters the original array by removing and returning the last element. This can be handy when you need to manipulate arrays by taking elements off the end.

Example: Here, pop() not only removes the last element (4) from the array but also returns it, allowing you to store or use the removed value if needed.

Javascript




let myArray = [1, 2, 3, 4];
 
let removedElement = myArray.pop();
 
console.log(myArray);          // Output: [1, 2, 3]
console.log(removedElement);   // Output: 4


Output

[ 1, 2, 3 ]
4


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads