pop() method removes the last element from an array and returns the removed element. In the example above, the last element in the
fruits array is “Mango”, which is removed by the
pop() method and stored in the
pop() method is called, the
fruits array is shortened by one element and no longer contains “Mango”. You can use the
console.log() method to view the updated array and the removed element.
var fruits = ["Banana", "Orange", "Apple", "Mango"]; // Removes the last element ("Mango") from the fruits array var lastElement = fruits.pop(); console.log(fruits); // Output: ["Banana", "Orange", "Apple"] console.log(lastElement); // Output: "Mango"
Leave a Reply