What is the pop() method used for in JavaScript Arrays ?
Last Updated :
31 Jan, 2024
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);
console.log(removedElement);
|
Share your thoughts in the comments
Please Login to comment...