I can complete the challenge easily. I just don’t understand the concept behind switching var to let.
Please explain as simple as possible on why it’s relevant for this problem. I particularly don’t understand what is meant by:
“This behavior will cause problems if you were to create a function and store it for later use inside a for loop that uses the i
variable. This is because the stored function will always refer to the value of the updated global i
variable.”
Maybe you can say in a much simpler way.
Tell us what’s happening:
Your code so far
function checkScope() {
"use strict";
var i = "function scope";
if (true) {
i = "block scope";
console.log("Block scope i is: ", i);
}
console.log("Function scope i is: ", i);
return i;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/compare-scopes-of-the-var-and-let-keywords