Disclaimer: This article is heavily inspired by a conference talk given by Phillip Roberts in EUConf 2014.

One thing critics of JS like to point out is that it is single threaded. When I started coding in JS, this confused me. Then how did JS handle async functions? How can we have things like setTimeout() that seems to work independently from the main thread? Hopefully by the end of the article, you will have answers to all these questions. Another goal of this article is to explain some core advanced concepts of JS: the call stack, the task queue and the event loop, which are fundamental to understanding how JS works within the browser.


Let’s say you wrote some JS code for a web app and shipped it. That JS code is run on the browser. How does the browser know what to do with it? The browser is able to run the JS code because it has a JS engine, which for Chrome is the V8 JavaScript engine. In simple terms, V8 knows how to convert JS code into machine code.

The browser is not just the JavaScript engine. It has its own set of APIs, called the Web APIs. setTimeout() is a part of this set of APIs. When you invoke functions such as setTimeout(), you are using an API function that the browser has exposed to you.

The browser is able to run the JS code because it has a JS engine, which for Chrome is the V8 JavaScript engine.

#software-engineering #programming #front-end-development #javascript #js

Understanding JavaScript Call Stack, Task Queue and Event Loop
1.50 GEEK