HomeToolsAbout a20k

Hoisting

What Is It

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

Hoisting Behavior of let and const

Variables defined with let and const are hoisted, but not initialized

Using let before declaration is a ReferenceError

/* ReferenceError */ name = "Sam"; let name;

Using const before declaration is a syntax error

/* Invalid Syntax */ name = "Gil"; const name;
© VincentVanKoh