title: IIFE vs function call (with parameters), inside a loop
body: <p>I am confused between using an IIFE vs using a normal function call (with parameters) inside a for loop. <br/></p>
  
  <p>Lets say the function is -</p>
  
  <pre><code>function print_doc_count(i){
      var str= "collection" + i.toString();
      db.collection(str).count(function(err, res){
          if(!err){
              console.log("docs in collection: "+str+" = "+res);
          }
      });
  }
  </code></pre>
  
  <p><br/>
  Example 1 (Without IIFE) - <br/></p>
  
  <pre><code>for(var i=1; i&lt;=10; i++){
      print_doc_count(i);
  }
  </code></pre>
  
  <p>Example 2 (With IIFE) - <br/></p>
  
  <pre><code>for(var i=1; i&lt;=10; i++){
      (function print_doc_count(i){
          var str= "collection" + i.toString();
          db.collection(str).count(function(err, res){
              if(!err){
                  console.log("docs in collection: "+str+" = "+res);
                  // str needed closure, it contains the value i!
              }
          });
      })(i);
  }
  </code></pre>
  
  <p><br/></p>
  
  <p>My question is what is the difference between Example1 and Example2 above and should one be preferred over the other (under what circumstances)?</p>