Open In App

How to Execute JavaScript Code ?

Last Updated : 15 May, 2023
Like Article

Javascript is a high-level, Just In Time compiled programming language which converts the entire machine code at once and then executes it immediately. Javascript code is executed by the Javascript Engine, which is separate software. Different browsers have their own implementation of JS Engine embedded in the browser like Google Chrome’s V8 Engine, Mozilla Firefox’s Spider Monkey, Safari’s SquirrelFish, and so on. Let’s explore how Google’s V8 Engine works.

JavaScript Code first goes into the Parser

  • Parser – It reads the JavaScript Code and parses it into a Data Structure called AST (Abstract Syntax Tree). AST is built by breaking down code into tokens and checks for the semantic and syntactic errors in the code. This tree is later used to generate machine code. Below is an example how what AST looks like. (Note – Actual AST looks more complex, this is just to explain to you easily).
  • Compilation / Execution – As mentioned earlier, Javascript is Just in timed Compiled language that makes use of both Interpreter and Compiler. Firstly, the generated AST goes to the Interpreter (Google called it Ignition) which provides the machine code. This machine code is now executed with the help of Call Stack. While the machine code is executing, the Compiler (Google called it Turbo Fan) tries to optimize the code and returns optimized machine code that will run later. The process of compilation and execution of code goes hand in hand.
  • Optimization – JS Engine first create a very unoptimized version of machine code so that execution of code can start as soon as possible. But in the background, the code is being optimized during already running program execution.

Note: As of now, Google’s V8 engine is the fastest Javascript Engine.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads