DEV Community

Gurnav224
Gurnav224

Posted on

How javascript engine excuted the Javascript code

JavaScript code runs within the JavaScript engine, such as V8 in Chrome or SpiderMonkey in Firefox. or Node.js
The engine will take care of the code execution life cycle like parsing, compilation, and execution

1. Parsing: In the first phase, the engine will parse the JavaScript code into an AST abstract
syntax tree structure

before parsing

let a = 5;
let b = 10;
let sum = a + b;
console.log(sum);

after parsing

Program
├── VariableDeclaration (let a = 5)
│ ├── Identifier (a)
│ └── Literal (5)
├── VariableDeclaration (let b = 10)
│ ├── Identifier (b)
│ └── Literal (10)
├── VariableDeclaration (let sum = a + b)
│ ├── Identifier (sum)
│ └── BinaryExpression (+)
│ ├── Identifier (a)
│ └── Identifier (b)
└── ExpressionStatement (console.log(sum))
├── MemberExpression (console.log)
│ ├── Identifier (console)
│ └── Identifier (log)
└── Identifier (sum)

2. compilation phase: in this phase engine compile the AST into byte code or machine code

  • modern day javascript engine use JIT or JUST-in-time compliation to make the excution faster

3. excution phase : in the excution phase engine excuted the compile code
call stack keep track of the function call which function currenlty excuted

Top comments (0)