JavaScript knows two types of “foreach” loops:

  • for (idx in values) iterates over the indices of the array
  • for (val of values) iterates over the values of the array

Example:

var values = ["a", "b", "c"]; // indexed from 0 to 2
// Access elements by index
for (var idx in values) {
  console.log("Index: " + idx + " - Value: " + values[idx]);
}
// Access elements directly
for (var val in values) {
  console.log("Value: " + val);
}

 References

  • [1] MDN on for .. of