-2
function A() {
  let local = "local";

  for (let i = 0; i < 4; i++) {
    var var_variable = "var_variable";
    const k = 10;
    {
      var block = "block";
    }
    function inside() {
      console.log("yo");
    }
    inside();
  }
}

A();

chrome dev tools

I know let and const are block scoped.

If you see from the image, let variable i has an own block and the for loop block which contain const scoped to that block and var which is scoped to that execution context.

My question is does let get copied into the for loop block in each iteration and destroyed?

Vegeta
  • 461
  • 2
  • 6
  • I think the specs should answer your question : https://www.ecma-international.org/ecma-262/6.0/#sec-for-statement-runtime-semantics-labelledevaluation – Seblor Jan 10 '21 at 10:33

1 Answers1

-1

let basically allows that variable be used within that scope, as you mention you're familiar with. To answer your question, it's valid within the for loop until the end of the scope of that block of code. It's not "copied in" every time, the variable i is made at the call of the for() loop, then cleared / lost when the for loop ends.

See https://www.w3schools.com/js/js_let.asp for more info on scopes

Guy S
  • 457
  • 4
  • 9