Javascript by default moves all declarations to the top of the current scope
/* valid code, hoisted automatically */ x = 3; // assignment ... // other code var x; // declaration /* also a valid code, no need to hoist, identical to above */ var x; // declaration x = 3; // assignment
let
and const
Variables defined with let
and const
are hoisted, but not initialized
let
before declaration is a ReferenceError/* ReferenceError */ name = "Sam"; let name;
const
before declaration is a syntax error/* Invalid Syntax */ name = "Gil"; const name;