JavaScript Event Loop & Call Stack Interview Questions and Answers
Trace JavaScript execution through the call stack, task queues, microtasks, and the event loop.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
Theoretical JavaScript Interview Questions
The event loop coordinates JavaScript jobs with work provided by the host, such as timers, network requests, and browser events. Starting an asynchronous operation does not make the operation itself run on the JavaScript call stack. When the host has a result, it queues a task or microtask, and the event loop runs it when the current job finishes. Promise reactions are microtasks and normally run before the next timer task. A long synchronous loop still blocks its JavaScript thread and can freeze the page.
The event loop coordinates the call stack with queues managed by the host environment. After the current job finishes, Promise reactions in the microtask queue are processed before the event loop takes the next task, such as a timer or browser event. This lets JavaScript respond to asynchronous work without blocking while it waits, but a long synchronous job can still freeze the page. That's your event loop event loop. A host schedules callbacks when work is ready; it does not make the callback interrupt the current JavaScript job. Gotcha: microtasks (Promises) normally run before a timer task, which often matters in interviews.
The call stack is JS's way of tracking where it is in execution - every time you call a function, a new frame gets pushed on the stack, and when it returns it gets popped off. Since JS is single-threaded, the stack can only process one thing at a time. If you see "Maximum call stack size exceeded", that's a stack overflow from infinite recursion. The stack is also what you see in your browser's debugger as the call trace when an error is thrown.
Microtasks are high-priority tasks such as Promise.then(), catch(), finally(), and queueMicrotask(). Macrotasks are regular tasks such as setTimeout(), setInterval(), and many browser events. After the current call stack finishes, JavaScript runs all microtasks before it moves to the next macrotask.
setTimeout(() => console.log("timeout"), 0);
Promise.resolve().then(() => console.log("promise"));
// Output:
// promise
// timeout
Microtasks are drained immediately after the current synchronous code finishes and before the event loop continues to the next timer or browser task. This is why Promise callbacks usually run before setTimeout(..., 0). If your code keeps adding many microtasks, they can delay timers, rendering, and other queued work.
A page's JavaScript runs one piece at a time on its event-loop thread. The browser may do other work concurrently, but callbacks are scheduled onto that thread.
The call stack tracks currently executing function calls, with the most recent call at the top. A function returns or throws before execution continues in its caller.
A task is scheduled work such as a timer or event callback that runs when the current stack is clear. Tasks differ from microtasks, which are generally drained at checkpoints before another task.
A microtask is deferred work scheduled by mechanisms such as promise reactions and queueMicrotask(). Browsers generally drain microtasks after the current stack and before rendering or the next task.
A long loop occupies the JavaScript thread, delaying event handlers and rendering. Large computations can be divided into yielding chunks or moved to a worker when appropriate.
Promise reactions are microtasks while setTimeout schedules a task after its delay threshold. Pending microtasks are generally processed before the next task; zero delay does not mean immediate execution.
Rendering cannot interrupt currently executing JavaScript, and browsers may render between tasks after relevant microtasks. Scheduling is browser-controlled, so not every task guarantees a frame.
queueMicrotask schedules work at a microtask checkpoint without creating a promise solely for scheduling. Keep the work small because recursively queued microtasks can starve rendering and other tasks.
If each microtask schedules another, the queue may never empty and the event loop may not reach rendering or the next task. Yield through a task or cooperative scheduling mechanism to let the browser progress.
Browsers schedule tasks from different sources under event-loop rules that do not promise one universal ordering for every source. Depend on specified guarantees, not observations from one run.