Course contentsShow
← JavaScript
Lesson 1183 of 2,72327. The Event LoopFree lesson

Stack Frames and Function Context

Each stack frame holds a function's local variables, parameters, and return address until that function completes.

Stack Frames and Function Context

You'll learn what information JavaScript stores for each function call and how it keeps track of where to return.

What is a Stack Frame?

When JavaScript calls a function, it doesn't just jump into that function blindly. It creates a stack frame—a container that holds everything needed for that specific function call. Think of it like a sticky note that says: "Here's what this function is working with, and here's where to go back when done."

Each stack frame contains three key pieces of information:

  1. Local variables — Any variables declared inside the function
  2. Parameters — The arguments passed to the function
  3. Return address — Where to resume execution after the function completes

How Stack Frames Work

When you call a function, JavaScript creates a new frame and pushes it onto the call stack. When that function finishes, JavaScript pops the frame off, retrieves the return address, and continues from where it left off.

function greet(name) {
  const greeting = "Hello, " + name;
  return greeting;
}

function main() {
  const result = greet("Alice");
  // execution resumes here after greet() returns
}

main();

When greet() is called, its stack frame holds: name (parameter), greeting (local variable), and the return address (back to main). Once greet() completes, that frame is destroyed—the variables disappear, but the return address tells JavaScript to continue in main.

Why This Matters

Stack frames create isolated contexts. Each function call gets its own fresh set of local variables, even if you call the same function multiple times. This isolation is what makes functions predictable and reusable.

Key Takeaway: Each stack frame is a self-contained snapshot of a function's execution, holding its variables, parameters, and the location to return to when finished.