Javascript Call Stack and Callback Queue

Javascript 9 min min read Updated: Mar 09, 2026 Advanced
Javascript Call Stack and Callback Queue
Advanced Topic 3 of 15

JavaScript executes code using a mechanism called the Call Stack. Since JavaScript is single-threaded, it can only perform one operation at a time. The call stack manages the execution of functions in a structured order.

When asynchronous tasks occur, they are handled differently. Instead of blocking the call stack, completed asynchronous tasks are placed in a structure called the Callback Queue. These tasks wait until the call stack becomes empty before being executed.

What is the Call Stack?

The call stack is a data structure used by JavaScript to keep track of function calls. When a function is invoked, it is added to the top of the stack. When the function completes execution, it is removed from the stack.

Key Point: The call stack follows the Last In, First Out (LIFO) principle.

Example of Call Stack Execution

javascript function first(){ console.log("First function"); } function second(){ first(); console.log("Second function"); } second();
Output

First function

Second function

In this example, the second() function is added to the call stack first. Inside it, the first() function is called and added to the stack. After execution, functions are removed from the stack in reverse order.

What is the Callback Queue?

The callback queue stores callback functions from asynchronous operations such as timers, API requests, or event handlers. These callbacks wait until the call stack is empty before being executed.

Key Point: The callback queue holds completed asynchronous tasks waiting for execution.

Example with Callback Queue

javascript console.log("Start"); setTimeout(function(){ console.log("Timer completed"); }, 2000); console.log("End");
Output

Start

End

Timer completed

In this example, the timer function is handled by the browser's Web APIs. Once the timer finishes, the callback function is placed in the callback queue. It waits until the call stack becomes empty before being executed.

How Call Stack and Callback Queue Work Together

When an asynchronous operation finishes, its callback function is added to the callback queue. The Event Loop continuously checks whether the call stack is empty. When the stack becomes empty, the event loop moves the callback from the queue to the call stack.

  • The call stack executes synchronous code.
  • Asynchronous tasks are handled by Web APIs.
  • Completed tasks move to the callback queue.
  • The event loop transfers tasks to the call stack.
Key Point: The event loop acts as a bridge between the call stack and the callback queue.

Why This Concept is Important

Understanding the call stack and callback queue helps developers write efficient asynchronous code and avoid issues such as blocking the main thread.

  • Helps understand asynchronous programming
  • Improves debugging skills
  • Explains how JavaScript handles timers and API calls
  • Helps optimize application performance

Conclusion

The call stack manages the execution of synchronous JavaScript code, while the callback queue stores completed asynchronous tasks waiting to execute. Together with the event loop, they ensure that JavaScript applications run efficiently without blocking the main thread.

Understanding these concepts is essential for mastering asynchronous JavaScript and building high-performance web applications.

In the next tutorial, you will learn about Microtasks and Macrotasks in JavaScript, which explains how promises and timers are scheduled in the event loop.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators