Interactive online courses HTML Academy
2026-03-09 12:56 Diff

Not only numbers can be written in square brackets. Any expression can be formatted this way. In the end, this expression will be converted to a number.

Let’s practice this way of accessing array elements and alternate them with the same variable. To do this, you need to get a variable to store the current index of the array element. Let’s call it index and write the initial zero value in it.

If you now log the expression usersByDay[index] in the console, we’ll see the first value of the array. After all, now the variable holds zero, and the expression usersByDay[index] is equivalent to usersByDay[0].

If you change the value of the variable index from zero to one and access usersByDay[index] again, we’ll then get an element with the index 1, that is, the second element.

Look, the expression in brackets in both cases is the same: usersByDay[index]. But it returns different values for different conditions.

If we continue to increase index and read elements of the array usersByDay, then sooner or later we will go through all the elements.