var, let & const in Js | what is Temporal Dead Zone?

var & let

var & let both used for variable declaration in JavaScript but the different between them is var is global scope and let is block scope.

var a = 10;
var a = 100;
console.log(a);
100
It will print 100 but why? 

    when code is run, global execution context is created. this variable store in memory comp. var is global scope so JavaScript engine don’t understand this two variables are different he assumed both are same. so first store a : 10 then go to next line a : 100 he replace the value and store 100 , that’s why, it print 100.
let a = 10;
let a = 100;
console.log(a)
but in this case let is act like strict teacher😂 because he not allows two same name student / variables. it throw syntaxError identifier 'a' has already been declared.
var a = 10;
let x = 100;
console.log(x);
    when code is run execution context created. memory allocation phase starts, a put the value undefined and same like a let x store value undefined but in different place of memory.
console.log(x);
let x = 100;
    so in this case x is assign but in different memory location so js engine won’t found the x in global execution context, so it throw reference Error cannot access 'x' before initialization. this means you can not access variable before declaration. 

What is temporal Dead zone ? 

    Temporal Dead Zone is the time since when this let variable was hoisted and till it is initialized some value (undefined) the time between that is known as Temporal Dead Zone. 

let and const 

const is similar to let but little bit strict.

let a = 10;
const b = 100;
execution context created and memory allocation phase starts, during this both variable are store in different memory phase and assigned undefined means it happens in temporal dead zone. then assigned a : 10 and b : 100

You say the both are the same then what is the difference between them?

let a;
a = 10
console.log(a)
it is work perfectly. but in the case of const.
const b;
b = 100;
console.log(b)
you can not do the same thing in const it throw syntaxError: missing intializer in const declaration it means the value declare using const it can not change. 

How to avoid temporal dead zone? 

The best way to avoid temporal dead zone is push everything initialization and declaration on the top.
 so that’s all about var, let and const.