Synchronous code runs on the call stack immediately. Timers are handed to a Web API. When the delay ends, the callback joins the task queue. The event loop moves a queued callback onto the stack only after the stack is empty.

Call stack

    Web APIs

      Task queue

        Press Run to watch order: A, C, then B.

        Idle.

        Important JavaScript

        console.log("A");
        
        setTimeout(function () {
          console.log("B");
        }, 0);
        
        console.log("C");
        // Prints: A, C, then B
        // B waited in the task queue until the stack was clear.