O'Reilly logo
  • Edward Yasi thinks this is interesting:

n this loop, we create five divs to appear within a container. Each div is assigned an onclick handler that creates an alert box to display the index. Declaring i in the for loop creates a global variable named i, and then iterates it until its value reaches 5. When you click on any of these boxes, the alert says that i is equal to 5 for all divs, because the current value for the global i is 5 (Figure 2-2).


Cover of Learning React


See CodePen Learning React Declaring Variables in ES6