JavaScript Engine — JavaScript Interview Questions & Answers — Part 7
Every browser has a JavaScript Engine that is responsible for executing JavaScript code. For example, Google Chrome uses the V8 JavaScript engine. Other browsers use different engines, such as Spider Monkey in Firefox and JavaScript Core in Safari.
JavaScript engines need to implement the ECMAScript specification, which defines the standard for the JavaScript language. ECMAScript is the language specification, while JavaScript is the most well-known implementation of that specification.
Google’s V8 JavaScript engine is primarily written in C++. Its job is to take JavaScript code and execute it by generating machine code that the computer can execute.
How JavaScript Engine Works:
The JavaScript engine takes JavaScript source code as input and goes through several major stages, including parsing, compilation, and execution.
Parsing: During parsing, the JavaScript source code is broken down into smaller units called tokens. The parser analyses these tokens and generates an AST (Abstract Syntax Tree). The AST represents the structure and meaning of the JavaScript code.
JIT Compilation: Modern JavaScript engines generally use a combination of interpretation and compilation. This approach is commonly known as JIT (Just-In-Time) compilation.
For example, the V8 engine can start executing JavaScript code quickly and monitor the code while it is running. When it identifies code that is executed frequently, the compiler can optimize that code and generate more efficient machine code.
AOT (Ahead-of-Time) Compilation: It is a process where code is compiled before the application runs, typically during the build process. For example, Angular can compile application code and templates during the build, so the browser receives already-compiled JavaScript. This is different from JIT compilation.
Execution: Once the JavaScript engine has code that can be executed, it manages the execution of JavaScript using Memory Heap and Call Stack.
Memory Heap: The Memory Heap is an area of memory used by the JavaScript engine to store objects and other dynamically allocated data.
Get Beyond Coding’s stories in your inbox
Join Medium for free to get updates from this writer.
Interpreter Vs Compiler:
Interpreter: An interpreter takes the parsed code and starts executing it without first compiling the entire program into optimized machine code, which helps reduce the initial execution time.
Compiler: Compiler analyses the code and generates a more optimized machine code that can be executed efficiently by the CPU. Compilation can take some time, but the optimized code can execute faster, especially when the same code runs repeatedly.
JavaScript Runtime Environment:
A JavaScript Runtime Environment provides everything required to execute JavaScript code and interact with the outside environment. A JavaScript runtime environment generally consists of:
- JavaScript Engine — Executes JavaScript code.
- Runtime APIs — Provide additional functionalities to interact with the environment.
- Call Stack — Keeps track of currently executing JavaScript functions.
- Event Loop — Manages the execution of asynchronous callbacks.
- Task/Callback Queue — Holds tasks that are ready to be executed.
- Micro task Queue — Holds micro tasks such as Promise callbacks.
A browser provides a JavaScript runtime environment, which allows JavaScript code to execute and interact with the browser, such as the DOM, timers, network requests, storage, and other browser features. Node.js also provides a JavaScript runtime environment, allowing JavaScript to run outside the browser. Both browsers and Node.js provide JavaScript runtime environments, but they provide different environment-specific APIs.



