What is the event loop in JavaScript?
💡 Model Answer
The event loop is JavaScript’s core mechanism for handling asynchronous operations in a single‑threaded environment. It coordinates the call stack, task queues, and microtasks to ensure non‑blocking execution.
When a function is invoked, it is pushed onto the call stack. If the function performs an asynchronous operation (e.g., setTimeout, fetch, or a Promise), the operation is delegated to the browser’s Web APIs or Node’s libuv. Once the operation completes, its callback is placed in the appropriate queue: macro‑tasks (e.g., timers, I/O callbacks) or microtasks (e.g., Promise callbacks). The event loop continually checks the call stack; when it is empty, it first drains the microtask queue, then takes the next macro‑task from the queue and pushes it onto the stack. This cycle repeats, allowing asynchronous code to run without blocking the main thread.
Understanding the event loop is essential for writing efficient, responsive code, especially when dealing with callbacks, promises, and async/await patterns.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500