Javascript Array Example

This example uses JavaScript to display a list where each list item is an element of an array. All content in is generated using JavaScript. For more information, see https://www.w3schools.com/js/js_arrays.asp and https://www.w3schools.com/js/js_loop_for.asp

Example using for...of

Note that the correct syntax uses of and not in; using in will iterate over all of the properties of an object, and not items in a collection.

    for (element of array) {
        // do something with element
    }
  

Same example using for loop with index


    for (var i = 0; i < array.length; i++) {
        // do something with element array[i]
    }