How to pass a parameter of the for loop in the event handler?

for (var a = 0; a < arrOfTitles.length; a ++) {
 arrOfTitles[a].addEventListener('click', function() {

When you click on options alert is always sent to the last value of the parameter a, regardless of its location in the array. It is necessary that at a clique on the first element of the function alert passed parameter equal to the index of the array element which triggered the handler. How it can be implemented? Thanks in advance for your answers.
June 10th 19 at 14:54
2 answers
June 10th 19 at 14:56
You have the same variable a is common to all elements. Use let a = 0 instead ofvar a = 0.
June 10th 19 at 14:58
var -> let

Find more questions by tags JavaScript