What is the execution context in JavaScript and how is it created?

Instruction: Explain what an execution context is in JavaScript and the steps involved in its creation.

Context: This question evaluates the candidate's grasp of the fundamental concepts of how JavaScript code is executed, focusing on the creation and composition of execution contexts.

Official answer available

Preview the opening of the answer, then unlock the full walkthrough.

Initially, when a JavaScript engine runs a script, it creates a Global Execution Context (GEC). This is the default or base context where all code that is not inside a function or a module is executed. The creation of the Global Execution Context marks the first phase, known as the "Creation Phase." In this phase, the engine performs two critical actions: it creates a global object, which in a web browser is the window object, and it sets up a memory heap for storing variables and function references. Additionally, during this phase, the engine hoists function declarations and variable declarations to the top of their respective contexts. This means that functions...

Related Questions