Suggest an editImprove this articleRefine the answer for “setTimeout(fn, 0)”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`setTimeout(fn, 0)`** does not run the function immediately; it puts the `fn` callback in a queue to run after the current code, that is, asynchronously, on the next event loop iteration. **Key point:** JS first finishes the whole current stack (everything synchronous), and only then runs callbacks from the timer queue.Shown above the full answer for quick recall.Answer (EN)Image`setTimeout(fn, 0)` **does not run the function immediately**, as it might seem! It puts the `fn` callback into a **queue to run after the current code**, that is, **asynchronously**, on the next iteration of the event loop. --- ## What happens ```javascript setTimeout(() => console.log('timeout'), 0); console.log('main'); ``` Output: ```javascript main timeout ``` Even though the delay is 0, JS **first finishes the whole current stack** (everything synchronous), and only then runs the callbacks from the timer queue (callback queue). --- ## Why this happens JavaScript is **single-threaded**. All asynchronous operations (`setTimeout`, `fetch`, `Promise`) go into the event queue and run **only after the call stack becomes empty**. --- ## What `setTimeout(fn, 0)` is used for Deferring code execution to: - wait for the current operations to finish; - avoid blocking the interface; - run code "after everything else"; - let the browser update the DOM before execution. Example: ```javascript console.log('A'); setTimeout(() => console.log('B'), 0); console.log('C'); ``` Output: ```javascript A C B ``` --- ## Summary | What it does | Puts the callback in the queue for the next event loop iteration | |---|---| | Delay time | At least 0 ms, but actually a bit later | | Execution | After all synchronous code | | Used for | Deferred code, synchronization, avoiding UI blocking |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.