Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 1

Are classes hoisted in JavaScript

48 A class is not hoisted

fair

Use a class above its declaration and use a function above its declaration, and report the difference. Include typeof on the class name, an extends clause naming a class declared later, and an attempt to reassign the class name from inside the body.

Stuck? Three hints, smallest first
  1. 1. A class declaration is hoisted into scope but left uninitialised, exactly like let. A function declaration is hoisted complete.
  2. 2. typeof is safe on a name nobody declared, and not safe on one that is declared but still in its dead zone.
  3. 3. The extends expression is evaluated when the class declaration is reached, not later.

The answer

function classFirst() {
  let out;
  try { out = new Kiosk('A').tag; } catch (e) { out = e.constructor.name; }
  class Kiosk {
    constructor(id) { this.tag = 'kiosk-' + id; }
  }
  return out;
}

function functionFirst() {
  const made = kiosk('A');                    // fine: hoisted whole
  function kiosk(id) { return 'kiosk-' + id; }
  return made;
}

function typeofBefore() {
  let seen;
  try { seen = typeof Kiosk; } catch (e) { seen = e.constructor.name; }
  class Kiosk {}
  return seen;
}

function extendsLater() {
  let out;
  try { class Child extends Parent {} out = 'declared'; } catch (e) { out = e.constructor.name; }
  class Parent {}
  return out;
}

class Fixed {
  static rename() {
    try { Fixed = 1; return 'changed'; } catch (e) { return e.constructor.name; }
  }
}

const Anon = class {
  kind = 'anon';
};

class Kiosk {
  constructor(id) { this.tag = 'kiosk-' + id; }
}
const afterDeclaration = new Kiosk('B').tag;

console.log('class used first   :', classFirst());
console.log('function used first:', functionFirst());
console.log('typeof in the dead zone:', typeofBefore(), '| typeof undeclared:', typeof NoSuchClass);
console.log('extends a later class  :', extendsLater());
console.log('after the declaration  :', afterDeclaration);
console.log('rename from inside     :', Fixed.rename(), '| expression class name:', Anon.name);

Output


      
8 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 8 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

8 checks

test('a class cannot be used above its declaration', classFirst(), 'ReferenceError');
test('a function can, because the whole declaration is hoisted', functionFirst(), 'kiosk-A');
test('even typeof throws inside the dead zone', typeofBefore(), 'ReferenceError');
test('while typeof a name nobody declared is quietly fine', typeof NoSuchClass, 'undefined');
test('extends is evaluated at the declaration, so a parent declared later throws', extendsLater(), 'ReferenceError');
test('below the declaration everything works', afterDeclaration, 'kiosk-B');
test('the class name is a constant binding inside its own body', Fixed.rename(), 'TypeError');
test('a class expression is a value, and takes its name from what it is assigned to', [Anon.name, new Anon().kind], ['Anon', 'anon']);

"Classes are not hoisted" is shorthand and slightly wrong. The binding is hoisted — that is why typeof Kiosk throws rather than reporting 'undefined' — but it is uninitialised until execution reaches the declaration, exactly the temporal dead zone from exercise 2. A function declaration is hoisted with its body attached, which is why it can be called from above.

Two consequences. Ordering matters for extends: the parent expression is evaluated eagerly, so a file that declares subclasses before their bases throws at load. And inside the body the class name is its own immutable binding, so Fixed = 1 in a method is a TypeError while the same assignment from outside merely rebinds the outer name. The Anon.name assertion is the small pleasant surprise: an anonymous class expression infers its name from the variable it is assigned to.

Newsletter

New writing, when it lands.

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