Cheat Sheets
Scope and Hoisting · Study material
Scope and Hoisting Cheat Sheet
| Code | Result |
|---|---|
f(); function f() {} | works (hoisted) |
g(); var g = function () {}; | TypeError: g is not a function |
h(); const h = () => {}; | ReferenceError (TDZ) |
new C(); class C {} | ReferenceError (TDZ) |
function f() { return x; } let x = 1; f(); | 1 (called after initialisation) |
| inner function reads outer variable | allowed (scope chain) |
| outer function reads inner variable | ReferenceError |
| helper reads caller's local | not visible (lexical scope) |
function f(a = x) { const x = 1; } | a uses the OUTER x |
| top-level var in a module | not on globalThis |
| function + var with same name | function first, then the assignment wins |