Interview Prep10 min Read

Placement Prep 2026: The JavaScript Event Loop Explained for TCS NQT, Infosys SP & Google SDE-1

By DevLingo Team • Published

Hey future software engineers! Dreaming of landing that ₹12LPA+ role at a cutting-edge Bangalore or Hyderabad startup, or perhaps securing a coveted position at Google India, Infosys SP, or acing the TCS NQT? You're in the right place.

JavaScript is the backbone of modern web development, and mastering its nuances is absolutely crucial for your placement success. You've been told JavaScript is 'single-threaded.' One thread. One call stack. One thing at a time. And yet — you've probably seen code that *seems* to run concurrently. You set a `setTimeout` for 0 seconds, but it doesn't execute immediately. You fetch data, and your UI doesn't freeze. What gives?

This apparent paradox is where the legendary JavaScript Event Loop steps in. Understanding it isn't just about passing a tricky interview question; it's about writing efficient, non-blocking, and bug-free asynchronous code that distinguishes a good developer from a great one. Let's demystify it together.

The Core Mystery: Single-Threaded Yet Asynchronous?

Imagine JavaScript as a chef with only one hand (the Call Stack) to cook. This chef can only do one task at a time. If the chef is chopping onions, they can't simultaneously stir the curry. Simple, right? But what if the chef needs to wait for the oven to preheat? They don't just stand there doing nothing. They put the 'wait for oven' task aside, start kneading dough, and occasionally check if the oven is ready.

That 'putting aside' and 'checking' mechanism in JavaScript is powered by the Event Loop. It's the secret sauce that allows your single-threaded JS engine to handle asynchronous operations (like network requests, timers, or user interactions) without blocking the main thread, keeping your applications responsive and smooth.

Dissecting the Event Loop: Key Components You MUST Know

To truly grasp the Event Loop, you need to understand its main players. Interviewers, especially for Google SDE-1 or even Infosys SP, love to test your conceptual clarity here.

1. The Call Stack

This is where synchronous JavaScript code gets executed. When a function is called, it's pushed onto the stack. When it returns, it's popped off. JavaScript is strictly 'Last In, First Out' here. If the call stack is not empty, the Event Loop does *nothing*.

2. Web APIs (or Browser APIs / Node.js APIs)

These are not part of the JavaScript engine itself but are provided by the browser environment (or Node.js runtime). Think of them as specialized assistants that handle tasks JS can't do synchronously. Examples include:

  • `setTimeout()` and `setInterval()` (Timers)
  • `fetch()` (Network requests)
  • `addEventListener()` (DOM events)
  • `Promise` resolution/rejection (though Promises also involve microtasks)

When you call `setTimeout(callback, delay)`, the `setTimeout` function itself is executed on the Call Stack. But *scheduling* the `callback` to run after `delay` is handled by the Web API. Once the timer expires, the Web API doesn't push the `callback` directly to the Call Stack. Instead, it places it into a queue.

3. The Callback Queue (aka Task Queue / Macrotask Queue)

This is where callbacks from Web APIs (like `setTimeout` or DOM events) wait patiently. Once a Web API finishes its assigned task (e.g., `setTimeout` timer expires, or `fetch` request completes), it places its corresponding callback function into this queue.

4. The Microtask Queue

This is a special, high-priority queue introduced with Promises. It holds callbacks for `Promise.then()`, `Promise.catch()`, `Promise.finally()`, and `queueMicrotask()`. The Microtask Queue has a higher priority than the Callback Queue.

5. The Event Loop Itself

This is the orchestrator. Its *sole job* is to continuously monitor two things:

1. **The Call Stack:** Is it empty? 2. **The Queues:** Are there any pending callbacks in the Microtask or Callback Queues?

If the Call Stack is empty, the Event Loop first checks the **Microtask Queue**. If there are tasks there, it moves *all* of them, one by one, to the Call Stack for execution. Only after the Microtask Queue is completely empty does it then check the **Callback Queue**. If there's a task there, it moves *one* (yes, just one!) callback to the Call Stack for execution. Then the cycle repeats.

This priority system is a common trick question in TCS NQT and Infosys SP interviews!

How it All Works: A Step-by-Step Scenario

Let's trace a common example:

```javascript console.log('Start');

setTimeout(() => { console.log('setTimeout callback'); }, 0);

Promise.resolve().then(() => { console.log('Promise resolved'); });

console.log('End'); ```

Here’s what happens:

1. `console.log('Start')` goes to the Call Stack, executes, and prints "Start". Call Stack is now empty. 2. `setTimeout(() => { ... }, 0)` goes to the Call Stack. The `setTimeout` function is executed, and its callback (`() => { console.log('setTimeout callback'); }`) is handed over to the Web APIs. The Web API sets a timer for 0ms. The Call Stack is empty. 3. `Promise.resolve().then(() => { ... })` goes to the Call Stack. The `Promise.resolve()` part executes, and its `.then()` callback (`() => { console.log('Promise resolved'); }`) is immediately placed into the **Microtask Queue**. 4. `console.log('End')` goes to the Call Stack, executes, and prints "End". Call Stack is now empty.

***Event Loop Takes Over:***

5. The Call Stack is empty. The Event Loop checks the **Microtask Queue**. It finds `() => { console.log('Promise resolved'); }`. It moves this callback to the Call Stack. 6. The callback executes, prints "Promise resolved". Call Stack is now empty. 7. The Event Loop checks the **Microtask Queue** again (it prioritizes emptying it). It's now empty. 8. The Event Loop checks the **Callback Queue**. Meanwhile, the `setTimeout`'s 0ms timer has long expired, and its callback (`() => { console.log('setTimeout callback'); }`) has been moved from the Web APIs to the **Callback Queue**. 9. The Event Loop finds the `setTimeout` callback, moves it to the Call Stack. 10. The callback executes, prints "setTimeout callback". Call Stack is now empty.

**Output:** `Start` `End` `Promise resolved` `setTimeout callback`

This exact sequence, especially the `Promise` resolving before `setTimeout(..., 0)`, is a hallmark of interview questions you'll face for ₹12LPA+ roles!

Why This is a Google India SDE-1 / Infosys SP Interview Essential

Top companies like Google, Infosys (for their SP roles), and even TCS NQT often include questions related to asynchronous JavaScript, promise chaining, and the Event Loop. They want to see if you understand the underlying mechanics, not just how to use `async/await`.

  • **Predicting Output:** You'll often be given code snippets and asked to predict the order of console logs. This tests your understanding of Call Stack, Microtask Queue, and Callback Queue priorities.
  • **Debugging Async Issues:** Understanding the Event Loop helps you debug tricky scenarios where your `async` functions aren't behaving as expected.
  • **Optimizing Performance:** Knowing how non-blocking operations work allows you to write more performant UIs that don't freeze during heavy computations or network calls.

Mastering the Event Loop shows a deep appreciation for JavaScript's runtime environment, a trait highly valued in any software engineering role.

Elevate Your Placement Prep with DevLingo

Feeling ready to tackle these challenging concepts? DevLingo's gamified learning path for JavaScript takes you from basic syntax to advanced topics like the Event Loop, Promises, and `async/await` with interactive challenges and real-world coding problems. Prepare for your TCS NQT, Infosys SP, and Google India SDE-1 interviews like a pro.

Don't just memorize definitions; truly *understand* the 'why' behind JavaScript's behavior. Your dream job at a leading Bangalore or Hyderabad startup is within reach. Start coding with DevLingo today!

Frequently Asked Questions

How does the JavaScript Event Loop appear in technical interviews for companies like TCS NQT, Infosys SP, or Google India SDE-1?

Interviewers frequently use Event Loop concepts to test your fundamental understanding of asynchronous JavaScript. Expect questions that ask you to predict the output of code snippets involving `setTimeout`, `Promise.then()`, `queueMicrotask()`, and other async operations. They often look for your ability to trace execution flow through the Call Stack, Microtask Queue, and Callback Queue, especially the priority difference between microtasks and macrotasks. Explaining *why* a certain output occurs is key to scoring well.

What's a common mistake JS developers make regarding the Event Loop, and how can I avoid it in my placement prep?

A very common mistake is assuming that `setTimeout(callback, 0)` will execute its callback immediately or right after the current synchronous code. Due to the Event Loop's mechanism, the callback only runs *after* the entire current synchronous code block finishes AND after all tasks in the Microtask Queue are processed. Another common error is not understanding the strict priority: all microtasks (e.g., Promise callbacks) will execute before the Event Loop moves to *any* macrotask (e.g., `setTimeout`, DOM events) from the Callback Queue. To avoid this, always trace code mentally, prioritizing microtasks over macrotasks, and remember that `setTimeout` is not a guarantee of immediate execution, but rather a minimum delay before queuing.

🦊

Ready to stop scrolling and start coding?

Everything you just read is built into DevLingo as a playable challenge. Don't just learn it. **Own it.**

Download QR
Scan to Download