Return to Kroll Web Design home page

JavaScript Tutorials

« Return

pop method for arrays

The pop method removes the last element of an array, and returns it also.

Code:

<script type="text/javascript">
//<![CDATA[
myArray = new Array("one", "two", "three"); document.write("myArray before being popped:<br>"); for (i=0; i<myArray.length; i++) { document.write("myArray[", i, "] : ", myArray[i], "<br>"); } poppedElement = myArray.pop(); document.write("<br>", "poppedElement : ", poppedElement, "<br><br>"); document.write("myArray after being popped:<br>"); for (i=0; i<myArray.length; i++) { document.write("myArray[", i, "] : ", myArray[i], "<br>"); } //]]> </script>

Output:




©2010 Kroll Web Design    davidarthurkroll@verizon.net    781.910.3694