Javascript Engine and Call Stack explained
Captain Mcwiise
Posted on January 2, 2023
You can use playground.js to execute the code snippets.
The Javascript Engine
The main responsibility of any web browser is to serve static content (HTML and CSS) from the WWW, it means that a browser is just an interpreter of markup language and for being able to execute Javascript code, all browsers need to use an internal component called Javascript Engine which is developed by the browser's vendor.
Here is a list of JavaScript Engines for major Internet browsers:
- V8: JavaScript Engine developed by Google for Chrome
- SpiderMonkey: โ The JavaScript Engine used by Mozilla Firefox
- JavaScriptCore: โ Developed by Apple for Safari
- Rhino: โ Managed by Mozilla Foundation for Firefox
- Chakra: โ A JavaScript Engine for Microsoft Edge
The Call Stack
Javascript is a non-blocking single threaded programming language, which means that all functions we write in a program (.js file) are executed by the engine in one thread at runtime, where the engine keeps the order the functions are executed by using a LIFO (Last in First Out) data structure, and this data structure is what we call as the call stack.
Along with the call stack the engine comes with a memory heap, this is just a storage of variables, objects, constants and function definitions, being functions the ones which will get into the stack during the program execution.
The Global Execution Context GCE
Just for academic purposes, think about the global execution context like a main function which wraps all code we want to execute in a single .js
file. We cannot control the GCE, all we need to know is that this is always the first function which gets into the stack once the engine reads the code to run.
With all basic concepts in place, let's analyze the execution flow of the following program, keep in mind that the execution order does not rely on the order the functions are defined but the order they are invoked:
1. function a(){
2. console.log("A");
3. }
4. function b(callback){
5. console.log("B");
6. callback();
7. }
8. function end(){
9. console.log("End");
10. }
11. b(a);
12. end();
- The engine reads the
.js
file to run and then the GEC gets into the stack. - (Line 11)
function b(callback)
gets into the stack. - (Line 5)
console.log
function gets into the stack and prints out B in the web console. -
console.log
function is removed from the call stack after it is executed.
- (line 6)
function a()
gets into the stack. Notice that this is the same callback function. - (line 2)
console.log
function gets into the stack and prints out A in the web console. -
console.log
function is removed from the stack after it is executed. -
function a()
is removed from the call stack since its execution has ended. -
function b(callback)
is removed from the call stack since its execution has ended.
- (line 12)
function end()
gets into the stack. - (Line 5)
console.log
function gets into the stack and prints out End in the web console. -
console.log
function is removed from the stack after it is executed. -
function end()
function is removed from the stack since its execution has ended. - The global execution context is removed from the stack and the program ends.
Final Outcome
B
A
End
Posted on January 2, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
May 1, 2023