CodeZettaInterview Hub

JavaScript · Hoisting · Junior · Comparison

How does hoisting differ between function declarations and function expressions?

Short Interview Answer

Function declarations are fully hoisted and callable before their line; function expressions only hoist the variable binding (`var` → undefined, `let` → TDZ).

Detailed Explanation

A `function foo() {}` declaration is instantiated during creation, so `foo()` works anywhere in that scope. A function expression like `var foo = function() {}` only hoists `foo` as `undefined` until the assignment runs — calling early throws TypeError. With `const foo = () => {}`, the binding is in TDZ until initialization — calling early throws ReferenceError. Named function expressions expose the name only inside the function for recursion/debugging.

Example

console.log(typeof decl); // 'function'
console.log(typeof expr); // 'undefined'
function decl() {}
var expr = function () {};

Interview Tip

State the exact error types (TypeError vs ReferenceError) — strong junior signal.

Common Mistake

Saying all functions are hoisted the same way.

Did you know this answer?