Gordon  Taylor

Gordon Taylor

1624969920

How Javascript Is Single Threaded? in-Depth Explaination Of Event Loop and Call Stacks

I started learning Javascript about 2 years ago and wondering to find bits and pieces to explore how it internally works. I’d gotta heard words like v8 runtime engine which runs on browser but didn’t know anything about it and started doing my own research and experiment in the browser. Mostly all JS dev might have heard the term V8, chrome’s Runtime but some might even didn’t really know what that meant, what that did.

First of all, what is Javascript under the hood — the core component of NodeJS. Well, JS is single threaded, single concurrent language which has a call stack, an event loop, a callback queue and some other APIs. If we talk about the v8 runtime in the browser, the below one is simplified view of what it really is…

What is call stack?

You can think call stack to be a functional stack or execution stack when your Javascript code is run line by line. Unless the part of code is handed over to the callback queue, it fills the call stack of a run-time engine.

Consider the following image for the sake of understanding.

Here, first of all JS is designed to be run on the main() thread and so the main() function will be called first when we execute the above code all together. The entry execution point is console.log() statement and the execution will be halted until the argument provided inside does not finish its execution i.e. bar(6). Now, the bar(6) will be pushed on to the stack and then it calls the function foo(3*6) and hence comes on top. Now, when the execution of foo() finishes, it will return the result by popping the entry from stack and comes down to bar(6) which in turn will return entry to the console and it prints the result.

#javascript #event-loop #call-stack #web-development

How Javascript Is Single Threaded? in-Depth Explaination Of Event Loop and Call Stacks