Does setTimeout function runs in background or in parallel?

     console.log("hello async")
     }, 1000);
console.log("Hello sync");

setTimeout function console the hello async after delays of 1sec, after consoling Hello sync, so does it runs in background or in parallel.

>Solution :

To be clear, setTimeout() is actually window.setTimeout() and window is not part of the Document Object Model or a native object in JavaScript.

When you call setTimeout(), you are accessing a Web API and what happens next is that the browser will (asynchronously) begin a timer and when the time has elapsed, the callback function will be placed into the event queue. The next time the JavaScript runtime is idle, it will check the event queue and execute the next item in that queue.

So, while the callback function is executed by the JavaScript runtime (and thus the single thread of that runtime), the timer itself is handled in a separate thread of the browser.

Leave a Reply