'If let is not hoisted or they goes in temporal dead zone, then why this snippet is throwing error, when it could have just used the global reference

var a = 6;
{
  console.log(a)
  let a =55
  
} 

when i execute this snippet I get following as error message: ReferenceError: Cannot access 'a' before initialization

why the console.log(a) is not giving 6 as result.



Solution 1:[1]

The answer to your question is in your title: the variable is in a "temporal dead zone", which is just a name for the behaviour you're seeing.

Take this explanation on MDN:

Unlike variables declared with var, which will start with the value undefined, let variables are not initialized until their definition is evaluated. Accessing the variable before the initialization results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the initialization is processed.

The start of the block is the opening { and your console.log(a) comes before let statement, so it is in this "dead zone".

Why does it work that way? Because it helps programmers detect bugs in their code caused by confusion between variables with the same name in different scopes.

Solution 2:[2]

The nested block will create a new block scoped lexical environment.

The inner a variable is declared in that scope, but accessed before the initialization, thus the error.

The inner scope let a declaration overrides the var a declaration, so it's a different variable, as if you wrote it like this:

var a = 6;
{
  console.log(a2)
  let a2 =55
  
} 

.

Solution 3:[3]

To add to both above answers, the statement let a = 55 is actually two statements in one: let a - a declaration - and a = 55 - an assignment. Because of Javascript's hoisting rules, the declaration is processed on entry to the block, but the assignment retains its lexical position.

So the block:

var a = 6
{
  console.log(a)
  let a = 55
} 

is semantically equivalent to:

var a = 6
{
    let a
    console.log(a)
    a = 55
}

Because the local declaration of a masks the global variable of the same name, by the time console.log(a) is executed a has been declared but not yet assigned a value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IMSoP
Solution 2 František Žia?ik
Solution 3 Mario Camilleri