Have you ever wondered how JavaScript works behind the scenes?
This will be the simplest explanation ever.
What is a JavaScript engine or Runtime?
JavaScript engine executes JavaScript code.
Example includes V8 (Use in Google Chrome) and other web browsers.
The JS engine has
1) Call Stack: This is where our code is executed using the execution context
2)Heap: This stores Objects
Javascript codes are compiled or Interpreted or both compiled and interpreted.
Why Compilation and Interpretation?
Since every computer processor only understands 0's and 1's, the computer program needs to be converted into a machine code.
Programs can be converted into machine codes by Compilation or Interpretation
What is Compilation?
A Compiler converts the entire code/program into a machine(0's and 1's) code at once and writes to a binary file that can be executed by a computer.
2)Interpretation:
An Interpreter runs through the source code and executes it line by line. This is slow when compared to a compiler.
Over the years, Javascript has been referred to as an interpreter for reading codes line by line.
Since the introduction of Modern Javascript, Javascript now uses both a compiler and an interpreter called Just in Time compilation.
Steps Used in JS Engine
When you write a piece of code inside the Js engine, it passes through the following steps.
1)Js engine will parse the code and read it.
2)The code will be parsed into a data structure called the abstract syntax tree(AST)
3)The AST will split up the codes, check for syntax errors, then generate the machine code (0's and 1's)
5)The machine code will be compiled and executed.
Do you enjoy this? Please drop a comment and follow me @maakayjunior
Top comments (0)