The pop() method returns the element at the end of the array. In doing so, it deletes the item from the end of the array reducing the array length by one.
Elements are returned one at a time, even if several were pushed onto the stack together.
Arrays that were pushed onto the stack are returned as arrays.
Although this is very useful for programming stacks, it is not portable enough to deploy in a public facing site.
The result of this method is the item that was on the end of the stack.
// Create an array and test the Array.pop() method myArray = new Array("AAA", "BBB", "CCC"); document.write("Array<BR>") displayArrayAsTable(myArray); document.write("Array.pop()<BR>") document.write(myArray.pop()) document.write("<BR><BR>") document.write("Array after pop() call<BR>") displayArrayAsTable(myArray); // Display an array in a table function displayArrayAsTable(anArray) { myLength = anArray.length; document.write("<TABLE BORDER=1>"); for(myIndex = 0; myIndex < myLength; myIndex++) { document.write("<TR><TD>"); document.write(myIndex); document.write("</TD><TD>"); document.write(anArray[myIndex]); document.write("</TD></TR>"); } document.write("</TABLE><BR><BR>") }
Prev | Home | Next |
Array.length | Up | Array.prototype |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |