This question has already been answered:

Studying variables in js stumbled upon an interesting thing, namely here

let a = 10; (function(){ console.log(a); let a = 100; console.log(a); })(); 

If this is run through the transcompiler, then the first console will display undefined and it will be correct, because the ascent of the variable will work. If we run this code without a transcompiler, we get the error Uncaught ReferenceError: a is not defined

This is where the question arises, why does an error occur without a transcompiler because we have access to a variable from the outside? Explain this inaudibility.

PS Variable as well as functions float, but does let variable pop up? Different sources write differently, "someone pops up, someone doesn't" ... it's not clear ... So does it pop up? And how does the let variable work?

Reported as a duplicate at Grundy. javascript 3 Jul '17 at 11:03 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

1 answer 1

The second time the variable is redefined inside the parentheses, and after analyzing the interpreter, it knows that it will be defined inside the parentheses, so it does not look for it in the external array, but displays an error. http://learn.javascript.ru/let-const enter image description here

  • In general, the phrase: before the announcement, there are none at all, it is not entirely correct, they actually are - Grundy
  • @Grundy Well, this is not my phrase, but from Ilya's textbook :) - Spaider
  • I know :-) in my opinion there are a lot of problems with wording and terminology in it - Grundy
  • Well, in my opinion, the newcomers are so clearer, and if you want to understand more deeply, then you need to dig yourself - Spaider
  • Ilya's textbook is certainly one of the best, but some subtle points are not told, which is a pity! - MaximPro