How does JavaScript Engine work? And Execute?

The engine is simply a translator Just like we can interact with somebody who doesn’t know our language. And this special translator is called the engine to understands what they mean. Almost everyone that has worked with Javascript has to stimulate with the V8 engine. The shape of the JS engine. And most people know that Javascript is a single-threaded language. That is using a callback queue. we also hear phrases like JS is an interpreted language. So why we quite discover the whole of this? We can just write code without knowing each of these principles and we’ll be nice. If we understand how the engine operates underneath the hood we can design more optimized code. Code that compiler understands easily and run it quicker than our usual code.

Why need Javascript Engine?

const name = "Thor";

We just wrote some Javascript code. Assigning the “Thor” string to the name variable. Now the computer will understand this? No, the computer would have no sense whatever we quite write. The computer simply understands ones and zeros at the end of the day. So the computer doesn’t really identify what Javascript is. So how we able to talk useing a Javascript file so that the computer displays our name. This time comes to the picture engine. It helps us to give the machine dot.js extension file. And this machine is going to understand this file and tell the computer what to do. To display a Name. There are so many javascript engines that exist for example V8 engine, Spider monkey, KJS, etc. So anytime we use an engine we’re able to give it to a javascript file and this Javascript file gets understood by the engine and it allows this engine to communicate, and tell the machine the computer to do. what we ask it to do with Javascript. Presently, those engines are written by programmers, for example, the V8 engine is composed in C++. But why do programmers write these engines? 2008 marked a really pivotal moment in history when it comes to JavaScript. because Google released a V8 before then, Most browsers used engines that were very basic which meant that JavaScript was a little bit slow. Google had this problem. They had something called Google Maps and Google Maps as we know requires a lot of power. It does a lot of things we can ask for directions to zoom in and zoom out maybe even use Street View. And all the previous engines before it would make Google maps very very slow on the browser. And Google because they’re a search engine. They want everybody to use their search engine and they built a browser for them to control more of the market. So with Google Maps and their own Chrome browser, they said we’re going to write our own JavaScript engine. the V8 engine so that JavaScript runs way faster on the browser than it’s done previously in 2008 they released V8. But the big takeaway for us here is that really really smart people work on these engines so that our javascript runs as fast as possible on the browser on the server or any type of computer. So every day javascript gets faster and faster for us.

How it Process

  • AST (Abstract Syntax Tree).
  • Interpreter and Compiler.

Lexical analysis

AST (Abstract Syntax Tree)

Interpreter and Compiler,

Suppose you know two languages — Hindi and English. Now some friend of yours, knows only English and there is a nice book written in Hindi which she, unfortunately, can’t read, but desperately wants to ….

So she comes for your help…

Now you have two options -

  1. Read one line, translate it for her from Hindi to English. Read next line, translate, tell her English version of that line …And so on …
  2. Read the entire book and save the translated version in English, like a book. And give her that translated English version book.

Here the first method is an interpreter and the second is the compiler. The interpreter does line by line translation from source to the target language and does immediate execution .it does not store translated code.

In the compiler translated code is stored as ready to run code and so some memory is needed to store it. But the benefit of a compiler is, you don’t need it for translation each time you want the “same” source code translated, unlike the interpreter. ref: quora

Which one is the best interpreter or compiler?

Final Executing Output

Memory Heap

Example:

const name = "Thor";

In this example, we’re saying here is please allocate memory for the name variable and make this name variable point two in-memory heaps the value “Thor”

Call Stack

Example:

const sum = (a,b) => {return a + b}sum(10,5)

In this example when we run sum we simply on our call stack it’s going to just add the top of the stack and after we finish running this function it’s going to remove it from the call stack.

Conclusion

If interested, you can view more resources on Youtube and also read the Javapoint article.

Front End Developer