Skip to main content

Why is JS considered single-threaded?

Short answer

"Single-threaded" means that at any moment the call stack holds and executes only one piece of code.

You cannot compute something and, for example, handle an event at the same time - a new task starts only after the previous one finishes.


Example - sequential execution

javascript
console.log('A'); console.log('B'); console.log('C');

Output:

javascript
A B C

JS goes line by line: until A finishes, B won't start. Everything happens in a single thread.


What this means technically

The JavaScript engine (for example, V8) has:

  • the Call Stack - the stack of code being executed;
  • the Event Loop - the loop that manages asynchronous tasks;
  • the Callback Queue / Microtask Queue - queues of callbacks and promises.

But all of this serves a single thread of code execution.

Asynchronous tasks (network requests, timers, etc.) are not executed inside JS - they are handled by the browser (Web APIs) or the Node.js APIs, and JS just gets notified when the result is ready.


An example with asynchrony (to see the "single-threadedness")

javascript
console.log('1'); setTimeout(() => console.log('2'), 0); console.log('3');

Output:

javascript
1 3 2

Why?

  • JS first runs the synchronous code (1, 3);
  • then, once the stack is empty, the Event Loop takes a task from the queue (2).

Even with setTimeout(..., 0) - JS does not do two things at once.


Why this matters

Single-threadedness means:

  • Simplicity of code - no data races or complex synchronization;
  • A limitation - long operations (for example, loops, computations) block everything: the user interface "freezes".

But what about "multithreading" in the browser?

Although JS itself is single-threaded, the browser and Node.js have other threads (outside the JS engine):

  • network requests (XHR, fetch);
  • timers (setTimeout);
  • the file system (in Node.js);
  • Web Workers (a background thread, but with limits - no shared state).

JS just receives results from these threads, but it executes code itself only on one.


Visually

javascript
┌───────────────┐ │ Call Stack │ ← exactly one piece of code executes └───────────────┘ ↑ ┌──────────────────────┐ │ Event Loop │ ← checks whether the stack is free └──────────────────────┘ ↑ ┌──────────────────────┐ │ Callback / Microtask │ ← asynchronous tasks wait their turn └──────────────────────┘

SUMMARY

TermWhat it means
Single-threadedJS executes code sequentially, in one thread
AsynchronyAchieved through the Event Loop and queues
Web APIs / Node APIsProcess tasks in parallel, but outside the JS engine
MultithreadingOnly via Web Workers, with no access to shared data

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.