Return to Kroll Web Design home page

JavaScript Tutorials

« Return

reverse method for arrays

The reverse method, as you might expect, reverses the elements of an array, so that the order is just the opposite of what it had been.

Code:

<script type="text/javascript">
//<![CDATA[
myArray = new Array("a","b","c","d","e"); document.write("myArray before reversing the order of the elements:<br>"); for (i=0; i<myArray.length; i++) { document.write("myArray[", i, "] : ", myArray[i], "<br>"); } document.write("<br>"); myArray.reverse(); document.write("myArray after reversing the order of the elements:<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