title: scope of variables in JavaScript callback functions
body: <p>I expected the code below to alert "0" and "1", but it alert "2" twice. I don't understand the reason.  Don't know if it is a problem of jQuery. Also, please help me to edit title and tags of this post if they are inaccurate.</p>
  
  <pre><code>&lt;html&gt;
      &lt;head&gt;
          &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              $(function() {
                  for (var i=0; i&lt;2; i++) {
                      $.get('http://www.google.com/', function() {
                          alert(i);
                      });
                  }
              });
          &lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>