The pop method

The pop() method is used to remove the last element from an array. If you assign the result of the pop() method to a variable then the popped element will be assigned to the variable.

var Animals = new Array("dogs" , "cats" , "chickens");
var FavoriteAnimal = Animals.pop();
document.write("My favorite animals are "+FavoriteAnimal);

The code creates an array named Animals that contains three elements. The second line assigns the last element of the Animals variable to a new variable named FavoriteAnimal using the pop() method.

Leave a Reply