Skip to the solution
HardFrontend7 min read

What is the event loop?

473 views

The problem

Question presented to candidate: "In your own words, what is the event loop, and why does JavaScript need one?"

What a strong answer should cover:

  • JavaScript runs on a single thread -- one call stack, one line of code executing at any given instant, no true parallelism inside that thread.
  • Slow operations (a timer, a network request, a file read) are NOT run on that thread inline -- they are handed off to the surrounding environment (the browser or Node.js runtime), which notifies JavaScript via a callback once the work is done.
  • The event loop is the mechanism that takes those finished callbacks and runs them on the call stack, but only once the stack is completely empty -- it never interrupts currently running code.
  • This is what "non-blocking" and "asynchronous" actually mean in JavaScript -- not literal multi-threading, but a single thread that never sits idle waiting on slow work.
  • The precise queue mechanics (microtasks vs macrotasks, exact draining rules) are a deeper layer on top of this big picture -- worth naming that a fuller answer exists if the interviewer wants to go deeper.

Clarifying questions expected:

  • "Would you like the big-picture explanation, or should I go straight into the microtask/macrotask queue mechanics?"

Code / implementation expected: Optional -- a short snippet showing that code after a setTimeout call still runs before the timer fires is enough to illustrate the concept.

  • event loop
  • concurrency
  • single-thread
  • async

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptScript continues past an async call instead of waiting on it (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 147 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What is the temporal dead zone?

Continue