The versatile FOR keyword can be used to iterate over a different collections like array, objects and set of HTML elements.
FOR-IN statement iterates through all entries of an array, object, or values that implement
Each element returned by expression is visited exactly once. The current element is made available for further processing in the variable specified by
This will iterate over all elements from the array and make the current array element available in variable
i is not modified in this example but simply pushed into the result using the
The variable introduced by
FOR is available until the scope the
FOR is placed in is closed.
Another example that uses a statically declared array of values to iterate over:
Nesting of multiple
FOR statements is allowed, too. When
FOR statements are nested, a cross product of the array elements returned by the individual
FOR statements will be created.
In this example, there are two array iterations: an outer iteration over the array of months plus an inner iteration over the array of years. The inner array is traversed as many times as there are elements in the outer array. For each iteration, the current values of months and years are made available for further processing in the variable
FOR-WHILE variant specifies the repeated iteration as long as a boolean condition evaluates to true. The condition is evaluated before each iteration.
variableName variable holds a number of each iteration.