Beginner Node.js Interview QuestionsBeginnerConcept
Node.js · Question 4
What is the Node.js event loop?
Direct answer
The event loop is libuv's mechanism that runs phases (timers, poll, check, etc.) and executes queued callbacks when the JavaScript call stack is empty — enabling non-blocking I/O on a single main thread.
When V8 finishes synchronous code, the loop checks whether timers fired, I/O completed, or setImmediate callbacks are pending — then runs the associated functions. It repeats for the process lifetime.
Relationship to the browser
Both browser and Node have event loops, but Node's loop comes from libuv with different phases and APIs (fs, net, process). Browser loop is tied to rendering; Node has no DOM.
- Call stack first — synchronous code always runs to completion before any queued callback.
- Microtasks — process.nextTick and resolved Promises run between phases (Q6).
- Starvation — infinite nextTick recursion prevents I/O callbacks — production outage pattern.
event-loop-demo.js
console.log("sync start");
setTimeout(() => console.log("timeout"), 0);
Promise.resolve().then(() => console.log("promise microtask"));
process.nextTick(() => console.log("nextTick"));
console.log("sync end");Output
sync start sync end nextTick promise microtask timeout