Working Of Compiler In JavaScript

voidmain1812

Sourabh Singh

Posted on March 19, 2023

Working Of Compiler In JavaScript

JavaScript falls under the general category of "dynamic" or "interpreted" languages. But it is often misunderstood, in-fact JavaScript is Interpreted as well as Compiled language. Let's understand more about the execution of JavaScript code in the Blog

Working of Compiler in JavaScript

In traditional compilation process of a language, a chunk of source code undergo typically three steps before execution.

  1. Tokenizing / Lexing
  2. Parsing
  3. Code Generation

Tokenizing / Lexing:

Breaking up a Sting of characters into meaningful chunks called tokens.

Image to explain the process of tokenizing in compiler

Parsing:

A Stream (array) of tokens are turned into a tree of nested elements, which collectively represents the grammar structure of the program. This tree is called "AST" (Abstract Syntax Tree).
Abstract Syntax Tree

Code Generation:

The process of taking an AST & turning it into executable code.
AST to Code

But, the JavaScript Engine is vastly more complex than just these steps. Any Java-Script code is compiled even before it is executed.

Note: Execution of JavaScript Code is done by Interpreter.

Compilation before execution:

JavaScript compiles all the declarations & Assignments before the code is sent for execution so that the interpreter can skip those lines. This is done for faster execution speed.

let a = 3;    //The declaration and assignment of variable 
                will be compiled before execution.
console.log(a)  //This part will be executed by JavaScript 
                  interpreter
Enter fullscreen mode Exit fullscreen mode

Thanks for reading this blog.
Drop a like if you found this blog helpful and follow me for more of such blogs. šŸ˜„šŸ˜„

šŸ’– šŸ’Ŗ šŸ™… šŸš©
voidmain1812
Sourabh Singh

Posted on March 19, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Website cookies šŸŖ
webdev Website cookies šŸŖ

July 29, 2023

Working Of Compiler In JavaScript
webdev Working Of Compiler In JavaScript

March 19, 2023