Hey everybody, I have no problem completing this test but have a question about the introduction. This is the current one:
let printNumTwo;
for (let i = 0; i < 3; i++) {
if (i === 2) {
printNumTwo = function() {
return i;
};
}
}
console.log(printNumTwo());
console.log(i);
" Here the console will display the value 2
, and an error that i is not defined
.
i
is not defined because it was not declared in the global scope. It is only declared within the for
loop statement. printNumTwo()
returned the correct value because three different i
variables with unique values (0, 1, and 2) were created by the let
keyword within the loop statement."
//////////////////////////////////////////////////////////////////////////
I dont understand why there are only 3 values (0, 1 and 2) because I think it must’ve had the value “3” as well. Why i++ didnt function when i
reached the value 2?
Please help me.
Challenge: ES6 - Compare Scopes of the var and let Keywords
Link to the challenge: