JavaScript’s secret weapon: the event loop and callback queue”

sidverma
3 min readDec 11, 2022

Javascript is a single-threaded programming language, which means it can only process one task at a time. To handle asynchronous tasks, such as user input or network requests, Javascript uses an event loop and callback queue.

The event loop is a mechanism that constantly checks for new tasks and pushes them onto the call stack for execution. The call stack is a Last In, First Out (LIFO) data structure that stores the execution context of each function call. Whenever a function is called, it is pushed onto the top of the call stack, and when the function returns, it is popped off the top of the call stack.

The callback queue, on the other hand, is a queue of functions that are waiting to be called. When an asynchronous task completes, its callback function is pushed onto the callback queue. The event loop monitors the callback queue and, when the call stack is empty, it pushes the first callback function onto the call stack for execution.

This process continues until all tasks in the callback queue have been processed.

For example, consider the following code:

console.log('First');
setTimeout(() => {
console.log('Second');
}, 0);
console.log('Third');

When this code is executed, the console.log('First') statement is pushed onto the call stack and executed immediately. The setTimeout() function, however, is an asynchronous function that is pushed onto the callback queue instead of the call stack. The console.log('Third') statement is then pushed onto the call stack and executed, resulting in the following output:

First
Third

The setTimeout() function remains in the callback queue until the call stack is empty. At that point, the event loop pushes the callback function onto the call stack, which causes the console.log('Second') statement to be executed, resulting in the following final output:

First
Third
Second

Take another scenario:

The event loop is a mechanism that allows JavaScript to perform non-blocking tasks. It works by continuously checking the callback queue to see if any functions are waiting to be executed. If the call stack (where your code is executed) is empty, the event loop will take the first function in the callback queue and move it to the call stack to be executed.

Here’s an example of how the event loop works:

function first() {
console.log(‘first’);
}
function second() {
console.log(‘second’);
}
first();
setTimeout(second, 0); // non-blocking function
console.log(‘third’);

In the above code, the first() function is called first and added to the call stack to be executed. Then, the setTimeout() function is called with a delay of 0 milliseconds. This causes the second() function to be added to the callback queue, but it will not be executed until the call stack is empty. Finally, console.log('third') is called and added to the call stack.

In the above code, the first() function is called first and added to the call stack to be executed. Then, the setTimeout() function is called with a delay of 0 milliseconds. This causes the second() function to be added to the callback queue, but it will not be executed until the call stack is empty. Finally, console.log('third') is called and added to the call stack.

Here’s the order in which the functions are executed:

  1. first() is called and added to the call stack.
  2. console.log('third') is called and added to the call stack.
  3. The call stack is empty, so the event loop checks the callback queue.
  4. second() is moved from the callback queue to the call stack and executed.

As you can see, the second() function is not executed until the call stack is empty. This is because JavaScript is a single-threaded language and can only process one task at a time.

The event loop and callback queue are an important part of JavaScript’s asynchronous behavior. They allow JavaScript to perform non-blocking tasks and enable it to handle multiple tasks at the same time. This is why the event loop and callback queue are crucial to the performance of your JavaScript applications.

In summary, the event loop and callback queue are important mechanisms in Javascript that allow for asynchronous execution of code. The event loop checks for new tasks and pushes them onto the call stack for execution, while the callback queue holds functions that are waiting to be called. Together, these mechanisms enable Javascript to handle asynchronous tasks efficiently and effectively.

--

--