How JavaScript code actually runs?

·

2 min read

This article is basically on describing the "behind the scenes" of the running of JavaScript code.

If you have a little knowledge of programming then you may be assuming that you write a code in JavaScript and then it is converted into machine code and further the output is being displayed. You are absolutely right but till some extent, below is the actual diagram of elements which helps is running the JavaScript code.

Code --> Parser --> AST --> Machine Code --> Output

Here, Code - is the source code written by a programmer to run a program. Parser - is the element which parse the code and convert the source code into machine code. AST - known as Abstract Syntax Tree is a Data Structure used by the Parser to convert the source code into machine code. Machine Code - is the code readable by the computer. Output - is the result of the program written by programmer.

JavaScript Self-Guide - Notepad 04-01-2021 12_02_47.png

For Example :-

function foo(x) {
if (x > 10) {
var a = 2;
return a * x;
}
return x + 10;
}

when you run your code the parse uses AST and below is the alike image of the above program in Abstract Syntax Tree.

ast.png