Hoisting
Essentially gives the impression variables exist even before declaration.
- Variables declared with
var
are hoisted to the top of their function scope, but their initialization remains where the code originally places it. Before initialization, the variable isundefined
. - Variables declared with
let
andconst
are also hoisted to the top of their block, but they are not initialized. Accessing them before declaration results in aReferenceError
.