I know I can use the let keyword here to get the desired outcome, but I would like to know why clicking on the div items alerts #5 for each div.
var div;
var box = document.getElementById('box');
for (var i = 0; i < 5; i++) {
div = document.createElement('div');
div.onClick = function() {
alert('this is box # ' + i);
}
box.appendChild(div);
}
<div id="box"></div>