Return to Kroll Web Design home page

JavaScript Tutorials

« Return

unShift method for arrays

The unShift method adds an element to the start of an array.

Code:

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