Course contentsShow
← JavaScript
Lesson 96 of 2,7233. Variables and ScopeFree lesson

Function Expressions vs Declarations

Why function expressions assigned to var/let/const follow variable hoisting rules, not function hoisting.

Function Expressions vs Declarations

You'll learn: Why function expressions follow different hoisting rules than function declarations.

The Core Difference

You've seen that function declarations are fully hoisted—you can call them before they appear in your code. But when you assign a function to a variable (a function expression), the hoisting behavior follows the variable's rules, not the function's.

Here's the key: the function itself isn't hoisted; only the variable declaration is.

With var

console.log(myFunc); // undefined (var is hoisted, initialized to undefined)
// myFunc(); // TypeError: myFunc is not a function

var myFunc = function() {
  return "Hello";
};

The var myFunc declaration is hoisted and initialized to undefined. The function assignment happens later, line-by-line. Before that assignment, myFunc exists but holds undefined, not a function.

With let and const

// console.log(myFunc); // ReferenceError: Cannot access 'myFunc' before initialization

const myFunc = function() {
  return "Hello";
};

With let or const, the variable is hoisted but stuck in the Temporal Dead Zone until the assignment line executes. Accessing it early throws an error.

The Analogy

Think of a function declaration as a printed recipe card (fully ready from the start). A function expression is like writing a recipe on a sticky note—the note exists (hoisted variable), but it's blank (undefined or TDZ) until you actually write the recipe on it.

Key Takeaway: Function expressions assigned to var, let, or const follow variable hoisting rules—only the variable name is hoisted, not the function value. Always define function expressions before calling them.