JavaScript event loop interview questions.

Event loop questions test whether you can predict when code runs, not just what it does. These start with the classic setTimeout versus promise ordering puzzles, move on to await timing and nested callbacks, and end with code you build on top of the loop: drift-free timers, task queues, fake timers for tests and batching within a tick.

2 of these questions have free answers; the rest are in Pro. Updated .

22

questions

Easy
6
Medium
11
Hard
5

What interviewers look for

  • You can order synchronous code, microtasks and timers in a snippet, and explain why.
  • You know that an async function runs synchronously until its first await.
  • You know why a long task or an endless chain of microtasks freezes the page.
  • You can split heavy work so the browser can still paint and respond to input.

Concept questions, answered

The definitions and comparisons interviewers open with. Free to read, each with a full answer and examples.

What is the event loop in JavaScript?

The event loop is how JavaScript runs asynchronous code on a single thread. Synchronous code runs on the call stack. When the stack is empty, the engine runs every queued microtask (promise callbacks, await continuations, queueMicrotask), the browser may render a frame, and then the loop takes the next task, such as a timer or a click, and repeats. That order is why a promise callback runs before setTimeout(fn, 0).

Read the full answer

What are the call stack and execution context in JavaScript?

An execution context is the record the engine creates to run a piece of code: the global code, or one call of a function. It holds that code's variables, its scope chain and its this. The call stack is the list of active contexts. Calling a function pushes a new context on top, returning pops it, and the engine always runs the context on top.

Read the full answer

Synchronous vs asynchronous JavaScript: what is the difference?

Synchronous code runs one statement at a time, and each statement must finish before the next starts, so a slow step blocks everything after it. Asynchronous code starts an operation, such as a network request or a timer, and carries on; the result is handled later by a callback, a promise or await. JavaScript runs your code on a single thread, so asynchronous APIs are how it waits without freezing.

Read the full answer

What is a Promise in JavaScript?

A Promise is an object that stands for the eventual result of an asynchronous operation. It starts as pending and settles once, either fulfilled with a value or rejected with a reason. You attach handlers with then, catch and finally, and they run later as microtasks, after the current code has finished.

Read the full answer

Async/await vs promises: what is the difference?

async/await is not a separate mechanism. It is syntax built on promises. An async function always returns a promise, and await pauses that function until a promise settles, then gives you its value or throws its error. The same work can be written either way. await mainly reads like synchronous code.

Read the full answer

setTimeout vs setImmediate vs process.nextTick

In Node.js, process.nextTick callbacks run as soon as the current operation finishes, before promise callbacks and before the event loop moves on. setImmediate runs in the event loop's check phase, right after I/O polling. setTimeout(fn, 0) runs in the timers phase once at least 1ms has passed. So inside an I/O callback, setImmediate always beats setTimeout(0), and nextTick beats both. Browsers have only setTimeout; setImmediate and nextTick are Node APIs.

Read the full answer

What is requestAnimationFrame used for?

requestAnimationFrame(callback) asks the browser to run your function once, just before it paints the next frame. It is used for JavaScript animations, for batching visual updates such as a scroll progress bar, and for throttling work to one run per frame. Because it follows the display's refresh rate and pauses in background tabs, it is smoother and cheaper than animating with setTimeout or setInterval.

Read the full answer

What are Web Workers in JavaScript?

A Web Worker is a script that the browser runs on a separate thread, so heavy computation does not block the main thread that handles clicks, scrolling and rendering. The page and the worker cannot share variables or touch each other's objects. They communicate by sending messages with postMessage, which copies the data.

Read the full answer

Medium11

Practise with tests and full answers.

Every question has a runnable editor, tests and a step-by-step solution. Passes from ₹149, paid once; nothing renews.

Get full access
Rehearse it in a timed JavaScript mock interviewA real editor with tests, the follow-ups an interviewer asks, and a score out of 100.

Frequently asked

How do I answer 'what is the event loop' in an interview?

Say that JavaScript runs your code on one thread, and the event loop decides what runs next: the current code until the call stack is empty, then every microtask, then possibly a render, then the next task such as a timer or an event. Then walk through a short setTimeout and promise example and its output.

What is the difference between microtasks and macrotasks?

Microtasks are promise callbacks, code after an await, queueMicrotask and MutationObserver callbacks. They all run as soon as the current code finishes. Macrotasks, or tasks, are timers, events and messages, and the loop runs one of them per turn. That is why a promise callback always beats setTimeout with a delay of 0.

Is the event loop the same in Node.js?

The idea is the same and promises still beat timers, but Node.js runs its loop in phases and has process.nextTick and setImmediate. process.nextTick callbacks run before promise callbacks, and setImmediate runs in the check phase after I/O.