Question: How can I get this

```
var i = 0;
var codes = [1, 2, 3];

for (var i = 0; i < codes.length; ++i)
{
    setTimeout(function(){alert(codes[i]);},100);
}

```

To `alert` `1`, `2` and `3` without using `let` keyword?

or bind an event on an element (**[example](http://jsfiddle.net/QyvNK/)**):

```
var i = 0;
var codes = [1, 2, 3];

for (var i = 0; i < codes.length; ++i)
{
    $("div").eq(i).click(function(){
        alert( codes[i] );
    });
}

```

Answer: 
Use a self-executing anonymous function and pass in `i` as an argument for variable closure.

```
var i = 0;
var codes = [1, 2, 3];

for (var i = 0; i < codes.length; ++i)
{
    (function( index ) {
        setTimeout(function(){alert(codes[index]);},100);
    })(i);
}

```

[Here's an example.](http://jsfiddle.net/5PGJx/)
You need to make use of a closure. I typically create a "generator" function that returns a function which is defined in the desired context:

```
var i = 0;
var codes = [1, 2, 3];
function callbackGenerator(i) { 
   return function(){alert(codes[i]);}
}

for (var i = 0; i < codes.length; ++i)
{
    setTimeout(callbackGenerator(i), 100);
}

```

More info and good examples here:
<https://developer.mozilla.org/en/JavaScript/Guide/Closures>