# Block Scope

```javascript
// ES5
var x = 12;

if( x< 15){
  var msg = 'Quantity is small';
}

console.log(msg); // Quantity is small

// ES6
let x = 12;

if(x< 15){
  let msg = 'Quantity is small';
}

console.log(msg); // ReferenceError: msg is not defined
```

## Temporal dead zone ( cann't access variable before it's defined)

```javascript
console.log(myVar); // "ReferenceError: myVar is not defined
let myVar = 10;
```

## Example

### ES5

```javascript
var arr = [];

for( var i =0; i < 10; i++){
  arr.push(function(){
    console.log(i);
  });
}

arr.forEach(function(fn){
  fn(); // (10) 10 // printed 10 times
});
```

### ES6 (using let)

```javascript
var arr = [];

for( let i =0; i < 10; i++){
  arr.push(function(){
    console.log(i);
  });
}

arr.forEach(function(fn){
  fn();
});

// output
0
1
2
3
4
5
6
7
8
9
```
