← Back to Node.js, page 10

nodejs-0188 · Easy fresher

Assume this file runs as a CommonJS script in sloppy mode. ```js console.log(typeof hoisted, typeof notHoisted); function hoisted() {} var notHoisted = function () {}; try { console.log(a); } catch (e) { console.log(e.name); } let a = 1; console.log(b); var b = 2; { function inBlock() { return 'block'; } } console.log(typeof inBlock); var x = 'outer'; (function () { console.log(x); var x = 'inner'; })(); try { new K(); } catch (e) { console.log(e.name); } class K {} console.log(typeof K); ``` What does it print, and why?

It prints: function undefined, ReferenceError, undefined, function, undefined, ReferenceError, function. Function declarations are hoisted with their body. var is hoisted as undefined. let and class exist but cannot be used before their line (the temporal dead zone), so they throw a ReferenceError. A var inside a function shadows the outer variable for the whole function, so the early read gives undefined. In sloppy mode a function declared in a block is also visible outside the block.

Full answer

Each line, in order:

  1. typeof hoisted is function, because the whole declaration is hoisted. typeof notHoisted is undefined: the var is hoisted and set to undefined, and the function is assigned later at its line. Output: function undefined.
  2. console.log(a) happens before let a = 1. The binding exists but is in the temporal dead zone, so reading it throws a ReferenceError. The catch prints ReferenceError.
  3. console.log(b) prints undefined. The var b is hoisted and initialised with undefined, and 2 is assigned later.
  4. inBlock is declared inside a block. In sloppy mode (the old web-compatible rule) the function is also available in the surrounding function scope after the block runs, so typeof inBlock is function. In strict mode it would be undefined.
  5. Inside the function, var x is hoisted to the top of that function and shadows the outer x. At the time of the log it is still undefined, so it prints undefined, not outer.
  6. new K() before the class line throws a ReferenceError, because class declarations are also in the temporal dead zone.
  7. After the class line, typeof K is function (a class is a function).

Rules: function declarations hoist with their value, var hoists as undefined, let, const and class hoist but stay uninitialised until their line. Reading them early is an error, even with typeof.

javascript
console.log(typeof hoisted, typeof notHoisted);
function hoisted() {}
var notHoisted = function () {};

try { console.log(a); } catch (e) { console.log(e.name); }
let a = 1;

console.log(b);
var b = 2;

{ function inBlock() { return 'block'; } }
console.log(typeof inBlock);

var x = 'outer';
(function () { console.log(x); var x = 'inner'; })();

try { new K(); } catch (e) { console.log(e.name); }
class K {}
console.log(typeof K);

The snippet is the puzzle itself. The output is one line per console.log call. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): function undefined ReferenceError undefined function undefined ReferenceError function

Key points

  • Function declarations are hoisted with their body.
  • var is hoisted and initialised to undefined.
  • let, const and class are in the temporal dead zone until their line.
  • A var in a function shadows outer variables for the whole function.
  • Block functions behave differently in sloppy and strict mode.

Common mistakes

  • Thinking let is not hoisted at all.
  • Expecting the var function expression to be callable before its line.
  • Expecting the early log of x to print outer.
  • Thinking typeof is always safe, even in the dead zone.
  • Mixing up sloppy and strict mode results for block functions.

Follow-ups

  • What does strict mode change in this file?
  • What does a function declaration inside a block do in an ES module?
  • How does the TDZ help find bugs?

Tips

  • List the three kinds of hoisting.
  • Say 'temporal dead zone'.
  • Mention sloppy versus strict mode.