title: Closures versus ES6 Let
body: <p>Trying to print series of numbers inside for loop using closures and with let:</p>
  
  <p>Consider the following example:</p>
  
  <pre><code>  for(var i=1; i&lt;10; i++){      
        setTimeout(function(){
          document.write(i);
        }, 1000);
    }
  </code></pre>
  
  <p>Output is: </p>
  
  <blockquote>
    <p>101010101010101010</p>
  </blockquote>
  
  <p>With Closures:</p>
  
  <pre><code>  for(var i=1; i&lt;10; i++){    
      (function(x){      
        setTimeout(function(){
          document.write(x);
        }, 1000);      
      })(i);
    }
  </code></pre>
  
  <p>Output is: </p>
  
  <blockquote>
    <p>123456789</p>
  </blockquote>
  
  <p>Without closure, just using ES6 let:</p>
  
  <pre><code> for(let i=1; i&lt;10; i++){      
        setTimeout(function(){
          document.write(i);
        }, 1000);
    }
  </code></pre>
  
  <p>Output is: </p>
  
  <blockquote>
    <p>123456789</p>
  </blockquote>
  
  <p>Trying to understand if we still need closures using IIFE blocks moving towards ES6?</p>
  
  <p>Any good example if we really need closures with ES6?</p>