JavaScript Synchronous Asynchronous [closed]

21 hours ago 3
ARTICLE AD BOX

JavaScript is generally considered a single-threaded, synchronous language, meaning it executes statements one by one.

However, in the following code, 3 is printed before 2:

console.log(1); setTimeout(function () { console.log(2); }, 2000); console.log(3);

My understanding is that JavaScript should not execute the next statement until the current one has completed.

So why does console.log(3) execute before console.log(2)?

How does setTimeout() work internally in relation to JavaScript’s synchronous execution model and the event loop?

Read Entire Article