CodeZettaInterview Hub

JavaScript · Hoisting · Mid-Level · Concept

Are ES6 classes hoisted? Can you use a class before its declaration?

Short Interview Answer

Class declarations are hoisted into the scope but remain in the TDZ until initialized — using them early throws ReferenceError.

Detailed Explanation

Unlike function declarations, `class Foo {}` cannot be invoked before the declaration line even though the binding is registered. Until initialization, `Foo` is in the TDZ. Class expressions follow the same rules as other `let`/`const` bindings depending on how they are declared. Extending a class also requires the superclass to be already initialized. This design avoids partially-constructed constructors and matches `let`/`const` temporal semantics.

Example

const x = new Person(); // ReferenceError
class Person {}

Interview Tip

Compare directly to function declarations — classic interview contrast.

Common Mistake

Claiming classes are not hoisted at all; the binding is hoisted but uninitialized.

Did you know this answer?