An array can act just like a stack. A stack is a last-in-first-out (LIFO) structure.
The insertion (called a push) and removal (called a pop) in a stack occur at the top of the stack. The push() method accepts any number of arguments and adds them to the end of the array returning the array's new length.
The pop()
method removes the last item
in the array, decrements the array's length, and returns
that item.
pop() method changes the length of an array.
To remove the first element of an array, use the shift() method.
pop() |
Yes | Yes | Yes | Yes | Yes |
array.pop()
None
pop() method returns the removed array item.
var colors = new Array(); //create an array
var count = colors.push("A", "B"); //push two items
console.log(count); //2
count = colors.push("C"); //push another item on
console.log(count); //3
var item = colors.pop(); //get the last item
console.log(item); //"C"
console.log(colors.length); //2
The code above generates the following result.