Back to questions
JuniorTheoryCommon
What does the program print?
const fns = [];
for (var i = 0; i < 3; i++) {
fns.push(() => i);
}
console.log(fns[0](), fns[1](), fns[2]());3 3 3
- Expecting
0 1 2: withvarthere is oneifor the whole loop, and the functions read it when they are called, after the loop; onlyletgives each iteration its owni. - Expecting
2 2 2: the loop stops only wheni < 3is false, so after the last iterationi++makes it3.
- What does the program print if
varis replaced withlet? - What does
console.log(i)print right after the loop?