Is Javascript Single-Threaded?

Yes!! Javascript is a single-threaded language. This means it has one call stack and one memory heap. As expected, it executes code in order and must finish executing a piece of code before moving onto the next.

It’s synchronous, but at times that can be harmful. For example, if a function takes a while to execute or has to wait on something, it freezes everything up in the meanwhile.

Then How Is It Asynchronous?

Well, we have the Javascript engine (V8, Spidermonkey, JavaScriptCore, etc…) for that, which has a Web API that handles these tasks in the background. The call stack recognizes functions of the Web API and hands them off to be handled by the browser. Once those tasks are finished by the browser, they return and are pushed onto the stack as a callback.

Open your console and type then press enter. You'll see everything the Web API has to offer.

This includes things like ajax calls, event listeners, the fetch API, and setTimeout. Javascript uses low-level programming languages like C++ to perform these behind the scenes.

To Explore More:

Developer with 3 yrs of industrial experience in developing scalable web applications.