JavaScript Data structures: Array Loops

In the tableFor function, there’s a loop like this:

for (let i = 0; i < JOURNAL.length; i++) {

let entry = JOURNAL[i];

// Do something with entry


This kind of loop is common in classical JavaScript—going over arrays one element at a time is something that comes up a lot, and to do that you’d run a counter over the length of the array and pick out each element in turn.

There is a simpler way to write such loops in modern JavaScript.

for (let entry of JOURNAL) {

console.log(‘${} events.’);


When a for loop looks like this, with the word of after a variable defi­nition, it will loop over the elements of the value given after of. This works not only for arrays but also for strings and some other data structures. We’ll discuss how it works in Chapter 6.

Source: Haverbeke Marijn (2018), Eloquent JavaScript: A Modern Introduction to Programming,

No Starch Press; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *