Closure that captures variables from immediately-invoked parent function

Question | Dec 28, 2017 | hkumar 

In below code we create a function - foo - by immediately invoking a function expression. Note that the immediately-invoked function is passed global variable x as a parameter:

let x = 5;

let foo = (function (x) {

 let sum = 0;
 return function() {

   for(let i=0; i < x; ++i) 
    sum += i;

   return sum;
 };

})(x);

We call foo 3 times in a loop and store the results in an array. Also, the value of x is changed before calling foo:

// change x
x = 6;

// call foo 3 times and push results to an array
let results = [];
for(let i=0; i < 3; ++i)
 results.push(foo());

What will be logged as results below?

console.log(results.join(','));