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
console.log('A');
console.log('B');
console.log('C');Output:
A
B
CJS 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")
console.log('1');
setTimeout(() => console.log('2'), 0);
console.log('3');Output:
1
3
2Why?
- 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
┌───────────────┐
│ Call Stack │ ← exactly one piece of code executes
└───────────────┘
↑
┌──────────────────────┐
│ Event Loop │ ← checks whether the stack is free
└──────────────────────┘
↑
┌──────────────────────┐
│ Callback / Microtask │ ← asynchronous tasks wait their turn
└──────────────────────┘SUMMARY
| Term | What it means |
|---|---|
| Single-threaded | JS executes code sequentially, in one thread |
| Asynchrony | Achieved through the Event Loop and queues |
| Web APIs / Node APIs | Process tasks in parallel, but outside the JS engine |
| Multithreading | Only via Web Workers, with no access to shared data |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.