Skip to content
intermediatePhase 33 · Advanced JavaScript

Call Stack

Learn how the call stack manages function execution order.

30m
0 problems
Topic Progress0%

How the Call Stack Works

The call stack is a data structure that JavaScript uses to keep track of function calls. It follows the LIFO (Last In, First Out) principle—like a stack of plates, the last one placed on top is the first one removed.

When a function is called, a new frame is pushed onto the stack. When the function returns, its frame is popped off.

function first() {
  console.log('first start');
  second();
  console.log('first end');
}

function second() {
  console.log('second start');
  third();
  console.log('second end');
}

function third() {
  console.log('third');
}

first();

Call Stack Progression:

  1. first() called → pushed to stack
  2. second() called inside first → pushed to stack
  3. third() called inside second → pushed to stack
  4. third() returns → popped from stack
  5. second() returns → popped from stack
  6. first() returns → popped from stack

The call stack is now empty, and the program ends.

Stack Overflow

A stack overflow occurs when the call stack exceeds its maximum size. This typically happens with infinite recursion or deeply nested function calls.

// Infinite recursion - will cause stack overflow
function infinite() {
  return infinite(); // Calls itself forever
}

infinite(); // RangeError: Maximum call stack size exceeded

Common Causes

  1. Missing base case in recursion
// WRONG - no base case
function countdown(n) {
  console.log(n);
  countdown(n - 1); // Never stops!
}

// CORRECT - with base case
function countdown(n) {
  if (n <= 0) return; // Base case
  console.log(n);
  countdown(n - 1);
}
  1. Infinite loops with function calls
function loop() {
  loop(); // Recursive without condition
}
  1. Too many nested callbacks
// Deeply nested callbacks can also cause issues
for (let i = 0; i < 100000; i++) {
  (function nested() {
    nested();
  })();
}

How to Prevent

  • Always include a base case in recursive functions
  • Use iteration instead of recursion when possible
  • Set recursion depth limits if needed

Asynchronous Call Stack

JavaScript is single-threaded, meaning it has one call stack. However, it can handle asynchronous operations through the event loop, Web APIs, and callback queues.

When an async operation (like setTimeout or fetch) is encountered:

  1. The async function is pushed to the call stack
  2. The browser/web API handles the operation in the background
  3. The callback is moved to the callback queue
  4. The event loop pushes the callback to the call stack when the stack is empty
console.log('Start');

setTimeout(() => {
  console.log('Timeout'); // Runs after current call stack is empty
}, 0);

console.log('End');

// Output:
// Start
// End
// Timeout

Even with a 0ms delay, the timeout callback doesn't execute immediately. It waits in the queue until the call stack is clear.

function fetchData() {
  console.log('Fetching...');
  fetch('https://api.example.com/data')
    .then(response => response.json())
    .then(data => console.log(data));
  console.log('Done'); // Runs before fetch completes
}

fetchData();
// Output:
// Fetching...
// Done
// {data...} (after network request completes)

This is why understanding the call stack is crucial for writing predictable asynchronous code.

Practice Problems

0/3solved
Build Call Stack Component

Create a reusable React component implementing Call Stack. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Call Stack Testing

Write unit and integration tests for Call Stack using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Call Stack Performance

Optimize Call Stack for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What data structure does the call stack use?

Question 1 options

2. What causes a stack overflow?

Question 2 options

3. When does a setTimeout callback enter the call stack?

Question 3 options

4. Why is JavaScript called single-threaded?

Question 4 options

Flashcards

Question

What is the call stack?

Answer

A LIFO data structure that tracks function execution by pushing frames when functions are called and popping them when they return.

Question

What is a stack overflow?

Answer

An error that occurs when the call stack exceeds its maximum size, usually from infinite recursion or missing base cases.

Question

How does async code work with the call stack?

Answer

Async operations are handled by Web APIs. Callbacks wait in a queue until the call stack is empty, then the event loop pushes them to the stack.

Question

What is LIFO?

Answer

Last In, First Out - the principle where the last item added to a stack is the first one removed.

Question

What is Call Stack?

Answer

Call Stack is a key concept in frontend development.

Revision Notes

Key Takeaways

  • 1.The call stack uses LIFO (Last In, First Out) ordering
  • 2.Stack overflow happens with infinite recursion or missing base cases
  • 3.JavaScript is single-threaded with one call stack
  • 4.Async callbacks wait in a queue until the call stack is empty
  • 5.Understanding the call stack helps debug async behavior

Interview Tips

  • Walk through a recursive function step by step showing call stack changes
  • Explain why setTimeout(fn, 0) doesn't execute immediately
  • Discuss how to prevent stack overflow errors
  • Compare synchronous vs asynchronous execution using the call stack concept

Cheat Sheet

Call Stack Cheat Sheet

What is it?

  • LIFO data structure tracking function calls
  • One per JavaScript program (single-threaded)

How it works

  1. Function called → frame pushed to stack
  2. Function returns → frame popped from stack
  3. Stack empty → program ends

Stack Overflow

  • Caused by infinite recursion or missing base cases
  • Fix: Always add a base case, use iteration when possible

Async Behavior

  • Single call stack + Web APIs + callback queue
  • Callbacks wait in queue until stack is empty
  • Event loop bridges the gap