Return to Kroll Design home page

JavaScript Tutorials

 
« Return

shift method for arrays

The shift method removes the first element from an array, and also returns that element.

Code:

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

Output:



 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM