Return to Kroll Design home page

JavaScript Tutorials

 
« Return

arrays

An array is a variable containing one or more slots to put data into.

Code:

<script type="text/javascript">
//<![CDATA[
var fruits = new Array(); fruits[0] = "banana"; fruits[1] = "apple"; fruits[2] = "mango"; for(i=0; i<fruits.length; i++) { document.write(i, " : ", fruits[i], "<br>"); } document.write("<br>"); var fruits2 = new Array(); fruits2["banana"] = "soft"; fruits2["apple"] = "crisp"; fruits2["mango"] = "mysterious"; for(x in fruits2) { document.write(x + " : " + fruits2[x] + "<br>"); } document.write("<br>"); document.write("fruits2[\"banana\"] : ", fruits2["banana"], "<br>"); document.write("fruits2[0] : ", fruits2[0], "<br>"); // too bad you can't access a named array by element number //]]> </script>

Output:



 
« Return


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