What is the event loop in Node.js?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Node.js topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
The event loop is the core mechanism that enables Node.js to perform non-blocking I/O operations despite JavaScript being single-threaded. It continuously monitors the call stack and the callback queues, moving callbacks to the call stack when it is empty. The event loop has multiple phases executed in order: timers (setTimeout, setInterval callbacks), pending callbacks (I/O error callbacks), idle/prepare (internal), poll (retrieve new I/O events), check (setImmediate callbacks), and close callbacks. When Node.js starts, it initializes the event loop, processes the provided script (which may register timers and I/O), then begins the event loop. As long as there are callbacks to process or I/O operations pending, the event loop keeps running. Understanding the event loop is fundamental to writing efficient, non-blocking Node.js code.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.