TL;DR: setTimeout(fn, 0) doesn't run code instantly — it defers execution to the Macrotask queue, after all Microtasks and pending renders. Here's why relying on it is an anti-pattern and what to use instead.

We've all seen it in legacy frontend codebases or quick hotfixes. A piece of UI isn't rendering correctly, or a DOM element isn't ready yet, so a developer drops this in:

jssetTimeout(() => { doSomething(); }, 0);

It feels like magic because suddenly, the race condition disappears and the bug is solved. But do you actually know why it worked, or what it just did to your browser's execution priorities?

To master frontend engineering at scale, you have to look under the hood at the JavaScript Event Loop.