O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Iterating Through Arrays

var weekdays = ["Mon", "Tue", "Wed", "Thur", "Fri"]; for(var x=0; x < weekdays.length; x++){   document.write(day+"|"); } //Output is "Mon|Tue|Wed|Thur|Fri|"

The best method to iterate through JavaScript arrays is to use a for(init; condition; adjustment){LOOP_BLOCK} loop. The for() loop allows you to initialize a variable and then iterate through until a condition is met. To loop through the array, initialize an index and then stop the loop when the index is equal to the length of the array.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required