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

What is the Call Stack?

The call stack is a data structure that tracks function execution, pushing frames on entry and popping them on return.

What is the Call Stack?

You'll learn what the call stack is and how JavaScript uses it to keep track of function calls in your program.

The Core Idea

The call stack is JavaScript's internal to-do list for functions. It's a data structure that tracks which functions are currently running and in what order. When you call a function, JavaScript pushes a new "frame" (a record of that function call) onto the top of the stack. When the function finishes and returns, JavaScript pops that frame off the stack and goes back to whatever was underneath.

Think of it like a stack of cafeteria trays: you can only add or remove trays from the top. The last tray you put on is the first one you take off.

How It Works

When your code runs:

  1. Function called? → Push a frame onto the stack
  2. Function returns? → Pop that frame off the stack
  3. Another function called inside? → Push a new frame on top

The stack grows upward as functions call other functions, and shrinks as they return. JavaScript always executes whatever function is at the top of the stack.

Why It Matters

The call stack is why JavaScript knows where to "return to" after a function finishes. It's also why deeply nested function calls can cause a "stack overflow" error—the stack has a maximum size, and too many frames will exceed it.

Understanding the call stack helps you trace execution order, especially when functions call other functions, and is essential background for understanding asynchronous code behavior.

Key Takeaway: The call stack is JavaScript's mechanism for tracking function execution—it pushes frames when functions are called and pops them when they return, always executing the function at the top.