1
function btFun(){
  var arr = [];

  for (var i =0; i<3; i++){
    let j = i;
    arr.push(
      function(){
        console.log(j);
      }
    )
  }

  return arr;
}

I have question that involves let. When I execute the functions created with this code, I get 0 , 1, 2. And does that mean that let did create a copy value of that i iterator somewhere in memory and for each function it created reference to them?

enter image description here

  • The scope of `let` is the block, and each iteration introduces a new scope. – Barmar Jan 03 '18 at 16:30
  • I think you want to look into "block scoping" http://2ality.com/2015/02/es6-scoping.html How it is stored internally, is up to the JS Engine – epascarello Jan 03 '18 at 16:30
  • Right - it's not so much something that `let` does; it just gives you access to the scope that's already there (in modern JavaScript runtimes). – Pointy Jan 03 '18 at 16:31
  • Can someone recommend some entry to see how it does and not what it does? – Learn on hard way Jan 03 '18 at 16:35

0 Answers0