function buildFunctions2() {

	var arr = [];

	for (var i=0; i<3; i++) {
		let j = i;				// In ES6 we can use 'let j = i' to create a block scoped
		arr.push(				// variable that is assigned to a new spot in memory each time.
				function() {	// This results in the expected result of 0, 1, 2 when we call the functions
					console.log(j);	// on line 62, 63 & 64 below.
				}				
		);
	}

	return arr;
}
/*

In this example, the result of our 3 function calls on lines 19-21 are all 3.

This is again due to closures.

What happens is that buildFunctions() creates the array 'arr' and pushes the function objec on lines 8-10 to it, three times.

The array is returned and then assigned to the variable fs on line 17.

On line 19-21, we invoke the annonymous functions we pushed to each index of the array.

All three look for the variable 'i' but can't find it in the current execution context, so each of them look at their outer reference
and find it as '3' in the buildFunction() execution context, because this is the last value it was set to by our 
for loop before it stopped running.

*/
In this example, the result of our 3 function calls on lines 19-21 are all 3.
This is again due to closures.



The array is returned and then assigned to the variable fs on line 17.

On line 19-21, we invoke the annonymous functions we pushed to each index of the array.


In this example the result of our 3 function calls on lines 19 - 21 are all 3 . This is again due to closures . The array is returned and then assigned to the variable fs on line 17 . On line 19 - 21 we invoke the annonymous functions we pushed to each index of the array .
function buildFunctions2() {
	var arr = [];
	for (var i=0; i<3; i++) {
		let j = i;				
		arr.push(				
				function() {	
					console.log(j);	
				}				
		);
	}
	return arr;
}
function buildFunctions2 ( ) { var arr = [ ] ; for ( var i = 0 ; i < 3 ; i ++ ) { let j = i ; arr . push ( function ( ) { console . log ( j ) ; } ) ; } return arr ; }
In this example, the result of our 3 function calls on lines 19-21 are all 3.
In this example the result of our 3 function calls on lines 19 - 21 are all 3 .
// In ES6 we can use 'let j = i' to create a block scoped // variable that is assigned to a new spot in memory each time. // This results in the expected result of 0, 1, 2 when we call the functions // on line 62, 63 & 64 below.