Hello everyone ๐, I hope you are doing great.
So, today you are going to learn An Overview of JavaScript Engine, Heap, Call Stack ๐งโ๐ป.
Many JavaScript developers don't know how JavaScript works. If you are new to JavaScript ๐ถ, then this article will help to learn how JavaScript works. If you are an experienced ๐ด, JavaScript developer hopefully, this article will be a good refresher for you.
โ๏ธ JavaScript Engine
The JavaScript engine is a program that executes your JavaScript code. A popular example of a JavaScript engine is Google's V8 engine.
โ๏ธ V8 Engine
The V8 engine is an open-source, high-performance JavaScript and Web Assembly engine written in C++. The V8 engine is used inside Google Chrome, Node.js, and electron, among others.
The V8 engine has two main components
- Heap is an unstructured memory that is used for memory allocation of the variables and the objects.
- Call Stack is a LIFO data structure that is used for function calls that record where we are in the program.
๐ฅ Call Stack
JavaScript is a single-threaded programming language, which means it can do one thing at a time, and it has one Call Stack.
If you call a function, it's pushed on the top of the Call Stack, and when the function returns, it's popped from the top of the Call Stack.
Let's take an example.
Let's take another example that contains an error.
When the V8 engine encounters an error, it prints a stack trace. A stack trace is basically the state of the Call Stack.
Let's take another example that blows up the Call Stack ๐ฅ.
We can do this by using a recursive function.
A recursive function calls itself again and again. At some point in time, the number of function calls exceeds the actual size of the stack, and the browser detects this to take action by throwing an error.
I hope now you have a fair understanding of how JavaScript works.
In the next article, we will learn about Web APIs, Callback Queue, and Event Loop.
๐ Resources
What the heck is the event loop anyway? | Philip Roberts | JSConf EU
Thanks for reading! My name is Bipin Rajbhar; I love helping people to learn new skills ๐. You can follow me on Twitter if youโd like to be notified about new articles and resources.
Top comments (10)
Thank you . I understood it well ..
This clarify everything thanks
Thank you so much. I had no clear about it.
I am glad you liked it!!
Great short video diagrams. Looking forward to the rest in the series.
Nice article. The Mockups are great. What tool did you use to create the mockup diagrams?
After Effect
Nice one.
One question, since I donโt know the V8 engine so well: is it the browser that detects the stack overflow or the engine itself? I hope the engine, otherwise it puts the burden of handling the error in any application you embed the engine in.
Yes, the error is detected by the V8 engine ๐.
๐ Read This Doc: developer.mozilla.org/en-US/docs/W...
Really concise and clear explanation for anyone regardless of any prior experience on this. Nice read! ๐