Interview JS Part 3 -  Message Queue and Event Loop (Inspired by Philip Roberts)
Tags: javascript
Published: 2020-11-28 05:47:25

**Disclaimer**: This article is a mixture of two articles and important points are extracted from both and some new are also added to make it complete

1. [JS Event Loop](https://medium.com/front-end-weekly/javascript-event-loop-explained-4cd26af121d4)
2. [JS Event Loop Explained](https://blog.carbonfive.com/the-javascript-event-loop-explained/)

In this article, we will cover the following stuff:

1. What the heck is the event loop?

2. How do JS works?  V8 engine, JS is single-threaded then how is a callback in JS

3. What is JS? - A single-threaded non-blocking asynchronous concurrent language

4. JS - It has a call stack, an event loop, a callback queue, and some other APIs and stuff. 

**Architecture of Chrome**

![](https://miro.medium.com/max/1600/0*6T6KIVRkN9nWb3QU.gif)

Let me go through different parts of the chrome (image above)

* **Heap**: Unstructured region of memory

* **Stack**: Represents the single-threaded call stack.

* **Browser of Web API**:  They are not part of the JavaScript language itself, rather they are built on top of the core JavaScript language, providing you with extra superpowers to use in your JavaScript code

**Go through this below example carefully**:-

```javascript
function main(){
  console.log('A');
  setTimeout(
    function display(){ console.log('B'); }
  ,0);
	console.log('C');
}
main();
//	Output
//	A
//	C
//  B
```
![](https://miro.medium.com/max/1250/1*64BQlpR00yfDKsXVv9lnIg.png)

* The call to the main function is first pushed into the stack (as a frame). Then the browser pushes the first statement in the main function into the stack which is console.log(‘A’). This statement is executed and upon completion, that frame is popped out. Alphabet A is displayed in the console.

* The next statement (setTimeout() with callback exec() and 0ms wait time) is pushed into the call stack and execution starts. setTimeout function uses a Browser API to delay a callback to the provided function. The frame (with setTimeout) is then popped out once the handover to the browser is complete (for the timer).

* console.log(‘C’) is pushed to the stack while the timer runs in the browser for the callback to the exec() function. In this particular case, as the delay provided was 0ms, the callback will be added to the message queue as soon as the browser receives it (ideally).

* After the execution of the last statement in the main function, the main() frame is popped out of the call stack, thereby making it empty. For the browser to push any message from the queue to the call stack, the call stack has to be empty first. That is why even though the delay provided in the setTimeout() was 0 seconds, the callback to exec() has to wait till the execution of all the frames in the call stack is complete.

* Now the callback exec() is pushed into the call stack and executed. The alphabet C is displayed on the console. This is the event loop of javascript.

**Another Example**:-

![](https://blog.carbonfive.com/wp-content/uploads/2013/10/event-loop.png)

```javascript
function init() {
  var link = document.getElementById("foo");

  link.addEventListener("click", function changeColor() {
    this.style.color = "burlywood";
  });
}

init();
```

%[https://www.youtube.com/watch?v=8aGhZQkoFbQ]


Thanks for reading follow the Interview JS series for more core concepts of JavaScript.