Question: I am new to TypeScript and came accross this example:

```
for (let i = 0; i < 10 ; i++) {
    setTimeout(function() {console.log(i); }, 100 * i);
}

```

output: `1,2,3,4,5,6,7,8,9,10`

But 

```
for (var i = 0; i < 10 ; i++) {
    setTimeout(function() {console.log(i); }, 100 * i);
}

```

output : `10,10,10,10,10,10,10,10,10,10`

Can someone explain the reason please?

Answer: 
this is due to the scope difference
===================================

it is due the scoping difference between var and let. The first time the timeout actually can complete the console.log, i has already been changed by the loop to 10. (Essentially, you are stacking console.logs with various timeouts, they will only run when the time is done).

With let, which is `scoped` to the block, i takes the value inside console.log (because it is scoped to the block from which it called). In other words, as soon as it encountered this code, the variable became 'limited' to the function inside your timeout. This means that even though the loop changed the variable 'i' outside of it, it did not change the variable of the function.
@DylanMeeus is correct in that it's a scoping/closure issue. To help visualize this, this is what using "let" is compiled to in es5:

```
var _loop_1 = function(i) {
    setTimeout(function () { console.log(i); }, 100 * i);
};
for (var i = 0; i < 10; i++) {
    _loop_1(i);
}

```

Notice that the `i` in `_loop_1` is a new number instance.