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.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.