title: Please explain the use of JavaScript closures in loops
body: <p>I have read a number of explanations about closures and closures inside loops. I have a hard time understanding the concept. I have this code: Is there a way to reduce the code as much as possible so the concept of closure can be made clearer. I am having a hard time understanding the part in which the <code>i</code> is inside two parenthesis. Thanks</p>
  
  <pre><code>function addLinks () {
      for (var i=0, link; i&lt;5; i++) {
  
          link = document.createElement("a");
          link.innerHTML = "Link " + i;
  
  
          link.onclick = function (num) {
              return function () {
                  alert(num);
              };
          }(i);
          document.body.appendChild(link);
  
      }
  }
  window.onload = addLinks;
  </code></pre>