babel transpile `let` into `var`, how runtime know it should depend on block scope

Viewed 93

Here babel transpile let into var, how the javascript runtime know it should depend on block scope.

code:

for(let  i=10; i<11; i++){
  console.log(12);
}

babel compiles it into:

"use strict";

for (var i = 10; i < 11; i++) {
  console.log(12);
}

I used the try it out and choose es2015 on babel homepage.

1 Answers

The compiler will know if the function should be wrapped in another function call to create it's own closure. In your case it doesn't matter so the compiler knows another function call is unnecessary.

Related