Return to Kroll Web Design home page

JavaScript Tutorials

« Return

push method for arrays

The push method adds the specified element to the end of an array.

Code:

<script type="text/javascript">
//<![CDATA[
myArray = new Array("red", "green"); document.write("myArray before element is pushed onto it:<br>"); for (i=0; i<myArray.length; i++) { document.write("myArray[", i, "] : ", myArray[i], "<br>"); } document.write("<br>"); myArray.push("blue"); document.write("myArray after element is pushed onto it:<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