Explain the difference between var, let and const, including hoisting and the temporal dead zone.
They differ in scope, in re-assignability, and in what happens if you touch them before the declaration.
- var is function-scoped. It is hoisted to the top of its function and initialised to
undefined, so reading it before the declaration gives youundefinedrather than an error. It can be redeclared and reassigned. - let is block-scoped, can be reassigned, and cannot be redeclared in the same scope.
- const is block-scoped and cannot be reassigned. It does not make the value immutable — the properties of a
constobject can still be changed; only the binding is fixed.
The temporal dead zone is the region from the start of the block until the let or const declaration is evaluated. The binding is hoisted but uninitialised, so accessing it there throws a ReferenceError instead of quietly returning undefined. This is a deliberate improvement: it turns a class of silent bugs into loud ones.
Note: The classic demonstration is a for loop with a setTimeout inside. With var, all callbacks log the final value because they share one binding; with let, each iteration gets a fresh binding and they log 0, 1, 2.





