The simplest way to traverse arrays in js is to use for and then use the length of arr.length as the maximum value of for. Let's take a look at some useful examples
Example: for (){} Traverse the array
<script type="text/javascript"> <!-- var arr = new Array(13.5,3,4,5,6); for(var i=0;i<arr.length;i++){ arr[i] = arr[i]/2.0; } alert(arr); //--> </script>
Example: for in loop traverse the array
<html> <body> <script type="text/javascript"> var x var mycars = new Array() mycars[0] = "Saab" mycars[1] = "Volvo" mycars[2] = "BMW" for (x in mycars) { document.write(mycars[x] + "<br />") } </script> </body> </html
The above is the entire content of this chapter. For more related tutorials, please visit the JavaScript video tutorial !