The following code is supposed to log the correct number of button pressed. And it is supposedly does.
for (var i = 0; i < 5; i++) {
var btn = document.createElement('button');
btn.appendChild(document.createTextNode('Button ' + i));
btn.addEventListener('click', (function(i) {
return function() { console.log(i); };
})(i)); //line 4!
document.body.appendChild(btn);
}
What I don't get is what is the use of (i) on the line 4. It doesn't seem to pass anywhere as for me. Tell me please what to read so I could understand this.