JS interview question 1

JS Interview Questions

In JavaScript, one interview question that often pops up aims to test your understanding of asynchronous behavior and the event loop. Consider the following code snippet:

console.log("a");
console.log("b");

setTimeout(() => {
    console.log("c")
}, 0);

console.log("d");
console.log("e");

At first glance, you might expect the output to be "a", "b", "c", "d", "e", because "c" is set to log after 0 milliseconds. However, when you run this code, the output is actually:

a
b
d
e
c

Why does this happen? Let’s unravel the mystery behind this sequence by delving into the core mechanics of JavaScript’s event loop.

Synchronous vs. Asynchronous: The Key Difference

JavaScript operates on a single-threaded model, which means it can only execute one task at a time. These tasks are typically either synchronous or asynchronous:

  • Synchronous code runs line by line, blocking subsequent lines until the current one finishes.
  • Asynchronous code can be deferred, allowing the engine to continue executing other code while waiting for the asynchronous task to complete.

The Call Stack: Where It All Starts

The call stack is where JavaScript manages the execution of functions. Each time a function is called, it’s added to the top of the stack. Once the function completes, it’s removed from the stack. Let’s break down the synchronous parts of our code:

console.log("a"); // Logs "a" immediately
console.log("b"); // Logs "b" immediately after

At this point, the stack looks like this:

(Empty)

Each console.log is executed and popped off the stack sequentially.

Enter the Asynchronous: Web APIs

Next, we encounter an asynchronous function call with setTimeout:

setTimeout(() => {
    console.log("c")
}, 0);

Although the timer is set to 0 milliseconds, it doesn’t execute immediately. Instead, it’s handled by the Web APIs, which allow the browser to perform tasks asynchronously. The callback () => { console.log("c") } is sent to the Web APIs, which will manage the timing.

The Event Loop: Silent Coordinator

The event loop is the orchestrator that ensures asynchronous callbacks are executed in the right order. Here’s how it works:

  1. Monitor the Call Stack: The event loop constantly checks if the call stack is empty.
  2. Check the Task Queue: If the stack is empty, it looks at the task queue, where completed asynchronous tasks are queued up to be processed.
  3. Move to the Stack: If there are tasks in the queue, the event loop pushes the oldest task onto the call stack to be executed.

Completing the Sequence

After setTimeout is called, the next synchronous code continues:

console.log("d"); // Logs "d" immediately
console.log("e"); // Logs "e" immediately after

These statements are executed and removed from the stack one by one:

(Empty)

Now, the call stack is empty, and the event loop kicks in. It checks the task queue, finds our deferred callback () => { console.log("c") }, and pushes it onto the stack:

// Callback from setTimeout now executes
console.log("c"); // Logs "c" finally

This is why "c" appears last in the sequence.

Visualizing the Event Loop and Task Queue

To make this clearer, let’s visualize the process with the event loop and task queue in action:

  1. Call Stack (Chef’s Priority):
    • console.log("a")
    • console.log("b")
    • setTimeout(callback, 0) (callback handed to the kitchen helper)
    • console.log("d")
    • console.log("e")
  2. Task Queue :
    • callback (waiting for the call stack to be empty)
  3. Event Loop (Orchestrator):
    • Monitors the call stack.
    • Moves callback from the task queue to the call stack once it’s empty.

Conclusion: Event Loop

Understanding the event loop is crucial for mastering JavaScript and acing technical interviews. It explains how JavaScript handles asynchronous tasks, ensuring smooth and efficient execution. The sequence "a", "b", "d", "e", "c" is a direct result of the interplay between synchronous and asynchronous operations, managed seamlessly by the event loop.

Leave a Reply