Suggest an editImprove this articleRefine the answer for “Why is JS considered single-threaded?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**"Single-threaded"** means that at any given moment, only one piece of code sits in and is executed by the call stack. **Key point:** you cannot compute something and, for example, handle an event at the same time - a new task starts only after the previous one finishes.Shown above the full answer for quick recall.Answer (EN)Image## 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 | 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 |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.