Execution Context
I explored how JavaScript works internally, starting from its execution engine to the environment where code actually runs.
JavaScript runs on engines like V8, which read the code, allocate memory, and execute it step by step. This gave me a clearer understanding of what happens behind the scenes before any output appears.
A core part of this internal process is the Execution Context, which is created every time JavaScript begins to run code. I focused mainly on the two contexts that matter the most:
• Global Execution Context (GEC) – created when the file starts running.
• Function Execution Context (FEC) – created each time a function is called.
Each execution context goes through two stages:
Creation Phase
Before any line of code executes, JavaScript sets up everything it needs:
• Builds the Variable Environment
• Sets up the Lexical Environment
• Determines the value of this
• Creates memory space for variables
• Assigns default values in memory:
– var variables are set to undefined
– let and const are placed in the Temporal Dead Zone (uninitialized)
Recommended by LinkedIn
– Entire function declarations are stored with their full definitions
This phase prepares the memory structure for the upcoming code execution.
Execution Phase
Once memory setup is complete, JavaScript starts running code line by line:
• Assigns real values to variables
• Executes functions
• Updates memory as needed
• Evaluates expressions and returns values
Together, these two phases explain how JavaScript moves from reading code to actually producing output.
GEC: Browser vs Node.js
While the internal steps of creation and execution are the same, the global environment differs:
• In the browser, the global object is window.
• In Node.js, the global object is global.
Because of this, global variables and functions attach to different objects depending on where JavaScript is running, even though the execution process itself remains the same.
Understanding the engine, the execution context, and how environments differ made the internal working of JavaScript much more structured and logical for me.