Javascript
Mamta Kumawat  

Javascript Interview Question: What will be the output of the following code?

console.log("India");

const city = setTimeout(() => { 
  console.log("Kuchaman");
}, 0);

console.log("Rajasthan");

Understanding JavaScript’s Asynchronous Behavior: A Look at setTimeout() and the Event Loop

JavaScript is known for its asynchronous nature, which allows developers to manage multiple tasks without blocking the execution of other code. To better understand how this works, let’s break down the following code snippet and see how JavaScript handles both synchronous and asynchronous tasks.

console.log("India");

const city = setTimeout(() => { 
  console.log("Kuchaman");
}, 0);

console.log("Rajasthan");

Code Breakdown and Execution Flow

1.First console.log() Statement:

console.log("India");

This line is a simple synchronous operation. When JavaScript executes this line, it immediately prints "India" to the console. This happens because JavaScript’s execution model is synchronous by default, meaning it processes commands one after another in the order they appear unless stated otherwise.

2.The setTimeout() Function:

const city = setTimeout(() => {
console.log("Kuchaman");
}, 0);

Here, we have the setTimeout() function. This is a method used to execute a block of code after a specified delay. It takes two arguments:

  • The first is the callback function (() => { console.log("Kuchaman"); }), which will be executed after the delay.
  • The second argument is the delay in milliseconds (in this case, 0 ms).

The interesting thing about setTimeout() is that even though the delay is set to 0 ms, the callback function doesn’t execute immediately. Instead, it gets pushed to the event queue (or message queue) of JavaScript’s event loop. This happens because the event loop needs to first complete all the synchronous operations (like console.log("India") and console.log("Rajasthan")) before executing any asynchronous tasks (like the code in the setTimeout()).

So, even though the delay is 0, JavaScript will wait until all synchronous code has been executed before moving on to execute the asynchronous setTimeout() callback.

3.Second console.log() Statement:

console.log("Rajasthan");

Similar to the first console.log(), this line will also execute synchronously and print "Rajasthan" to the console right after the "India" statement, as there is no delay or asynchronous behavior involved.

The Output:

Let’s review what happens in the exact order:

  1. The first synchronous console.log("India") runs immediately.
  2. The setTimeout() is invoked and schedules the callback (console.log("Kuchaman")) to be executed after a delay of 0 milliseconds, but it doesn’t execute immediately. It’s placed on the event queue.
  3. The second synchronous console.log("Rajasthan") runs immediately after "India" and prints "Rajasthan".
  4. Now that the synchronous code has finished, the event loop picks up the callback from setTimeout() and executes it, printing "Kuchaman" to the console.

Final Output in Console:

India
Rajasthan
Kuchaman

The Key Takeaways:

  • Synchronous vs Asynchronous: console.log("India") and console.log("Rajasthan") are synchronous operations and run in the order they appear. setTimeout() is asynchronous, meaning its callback is queued up for execution after the current stack of operations is cleared.
  • Event Loop and Message Queue: Even with a delay of 0 ms, the setTimeout() callback is executed only after all the synchronous code has finished. This behavior is managed by JavaScript’s event loop, which ensures that asynchronous tasks do not block the execution of synchronous code.
  • Understanding Timing: While you might think that setting a delay of 0 ms would make the callback run immediately, it still gets delayed because the event loop has to wait until all synchronous code is finished first

Leave A Comment