0

Why output is 10 when use var and with let it works the loop. Edit the output is 11 but why let and var are different

var funcions = [];

for (var x = 0; x <= 10; x++) {
  funcions.push(function() {
    console.log(x);
  });
}

funcions.forEach(
  function(func) {
    func();
  }
);

I know that let, is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.

0 Answers0