ES6 - Compare Scopes of the var and let Keywords

In the example below why “let i=0” donot get the value of ‘3’ after the final iteration.
let printNumTwo;
for (let i = 0; i < 3; i++) {
if (i === 2) {
printNumTwo = function() {
return i;

Is this an actual challenge? From your title and the challenge I view with that title, the code I found for that challenge and your code are completely different.

I am a little confused why you wrote the code the way you did, but your answer is this line

if i is equal to two then you set the variable to a function that returns i. Once you hit a return in your loop then it stops the loop. You can see it further by changing it to i === 1 and you will notice that it will never be more than 1

Also, not sure you noticed but a part of your code will give you a reference error that i is not defined.

1 Like

Suppose i remove the if statement from the example then the answer of the console.log(printNumTwo()) would be 3?

Well you would have another issue in your loop

for (let i = 0; i < 3; i++)

Your loop is only going to keep going if i is less than 3. If you want it to be 3 then you could do i<=3

This way the loop will keep going as long as i is less than or equal to 3

Is there a reason you want to assign the variable to a function? Why not just assign i to the variable you created?