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/)
Use a [self-executing function](https://stackoverflow.com/questions/592396/what-is-the-purpose-of-a-self-executing-function-in-javascript):

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

```

Optionally using a `var` statement for more verbose code:

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

```

Or use a named function instead if you want to:

```
function foo(i) {
    setTimeout(function(){alert(codes[i]);},100);
}

for (var i = 0; i < codes.length; ++i) {
    foo(i);
}

```

Either example forms a closure, which introduces new variable scope. Alternatively, ES5 has [Function.bind](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind) that could be used on the anonymous function, but it is not universally supported in all browsers yet. (I'm thinking of IE.)