nazerke
7/23/2015 - 1:58 PM

prints out 6, 5 times.Because these are 5 different anonymous functions closing over exact same scope which is global scope that only has 1

prints out 6, 5 times.Because these are 5 different anonymous functions closing over exact same scope which is global scope that only has 1 i in it. Despite temptation to think that each function gets its own i.

<!DOCTYPE html>
<html>
  <head>
<script>

for (var i=1; i<=5; i++) {
	setTimeout(function(){
		console.log("i: " + i); 
	},i*1000);
}

</script>
  </head>
  <body>
    <!-- page content -->
  </body>
</html>