Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 1

Hoisting explained with typeof in JavaScript

145 hoisting, and the temporal dead zone typeof cannot dodge

hard

From the top of a function, probe every name declared below it with typeof inside try/catch: a var, a let, a const, a class, a function declaration, an arrow assigned to a const, and a name that is never declared anywhere. Report what each one gives. Then show that a let declared later in a block shadows an outer variable of the same name for the whole block, that default parameters have their own dead zone, and which of a var and a same-named function declaration wins.

Stuck? Three hints, smallest first
  1. 1. var declarations are hoisted and initialised to undefined. let, const and class are hoisted and left uninitialised.
  2. 2. typeof is only safe on a name that was never declared. On a name in its dead zone it throws like any other read.
  3. 3. Scoping is decided before any code runs, so a let anywhere in a block owns that name from the opening brace.

The answer

function survey() {
  const seen = {};
  seen.varBefore = typeof loose;
  try { seen.letBefore = typeof strictish; } catch (e) { seen.letBefore = e.constructor.name; }
  try { seen.constBefore = typeof fixed; } catch (e) { seen.constBefore = e.constructor.name; }
  try { seen.classBefore = typeof Stall; } catch (e) { seen.classBefore = e.constructor.name; }
  seen.fnBefore = typeof declared;
  seen.calledEarly = declared();
  try { seen.arrowBefore = typeof arrow; } catch (e) { seen.arrowBefore = e.constructor.name; }
  seen.neverDeclared = typeof nowhereAtAll;

  var loose = 'loose';
  let strictish = 'strictish';
  const fixed = 'fixed';
  class Stall {}
  function declared() { return 'callable above its own line'; }
  const arrow = () => 'not callable above its line';
  return seen;
}
const seen = survey();

const supplier = 'the outer supplier';
function shadowed() {
  let got;
  try { got = supplier; } catch (e) { got = e.constructor.name; }
  let supplier = 'the inner supplier';       // owns the name for the whole function
  return [got, supplier];
}

function defaults(a = b, b = 2) { return [a, b]; }
let defaultsBackwards;
try { defaultsBackwards = defaults(); } catch (e) { defaultsBackwards = e.constructor.name; }
const defaultsForwards = defaults(1);

function twins() { return 'first'; }
function twins() { return 'second'; }
const whichTwin = twins();

function varAgainstFunction() {
  const before = typeof mixed;
  var mixed = 1;
  const after = typeof mixed;
  function mixed() {}
  return [before, after];
}

console.log('survey    :', seen);
console.log('shadowed  :', shadowed());
console.log('defaults  :', defaultsBackwards, defaultsForwards);
console.log('twins     :', whichTwin, '| var vs function:', varAgainstFunction());

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('a var is hoisted AND initialised, so typeof reports undefined with no error', seen.varBefore, 'undefined');
test('let, const and class are hoisted but uninitialised, and reading one throws', [seen.letBefore, seen.constBefore, seen.classBefore], ['ReferenceError', 'ReferenceError', 'ReferenceError']);
test('so typeof is no longer a safe existence check', [seen.letBefore, seen.neverDeclared], ['ReferenceError', 'undefined']);
test('a function declaration is hoisted whole, and is callable above its own line', [seen.fnBefore, seen.calledEarly], ['function', 'callable above its own line']);
test('an arrow in a const is not: the const is what is hoisted, and it is in the dead zone', seen.arrowBefore, 'ReferenceError');
test('a let below owns the name for the whole block, so the read hits the dead zone', shadowed(), ['ReferenceError', 'the inner supplier']);
test('parameters initialise left to right in their own dead zone', [defaultsBackwards, defaultsForwards], ['ReferenceError', [1, 2]]);
test('two function declarations: the later one silently wins', whichTwin, 'second');
test('a var and a function of one name share a binding, and the function is what is hoisted', varAgainstFunction(), ['function', 'number']);

Hoisting is not code moving. Before a scope runs, the engine creates every binding it contains; the difference is only what each one starts as. A var starts as undefined, so reading it early is legal and useless. A function declaration starts as the finished function, which is why calling it above its definition works. let, const and class start uninitialised, and every read before the declaration line throws — that gap is the temporal dead zone, and it is temporal rather than positional: a function defined above the line but called below it reads the value fine.

The consequence people trip over is typeof. It used to be the one operator you could point at anything, and it still is for a name that was never declared, which is the check feature detection relies on. Point it at a let in its dead zone and it throws like a normal read. There is no syntactic guard left; the answer is to declare before use and stop probing.

The shadowing case is the one that reaches production. Adding let supplier at the bottom of a long function does not create a variable from that point down, it claims the name for the entire block, so a read forty lines above stops finding the outer value and starts throwing. Nothing about the failure points at the line you added. The last assertion is the fossil: a var and a function declaration of the same name are one binding, the function is installed during hoisting, and the assignment overwrites it on its own line, so typeof goes from function to number across two adjacent lines.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.