Creation Phase
Creates memory
Stores functions
Stores variables
Initializes var as undefined
Creates scope
Creates this
Understand how JavaScript creates execution environments, manages variables, resolves scope, and executes your code step by step.
Learn how Global Execution Context, Function Execution Context, Lexical Environment, Variable Environment, Scope Chain, and this binding work together behind the scenes.
Estimated Time
15 minutes
Difficulty
Intermediate
Progress
0% Complete
An Execution Context is the environment in which JavaScript code is executed. It contains all the information needed to run the code, including variables, functions, the value of this, and the scope chain.
Think of it as JavaScript's workspace for executing code. Every function call gets its own workspace. When the function finishes, that workspace is removed from the stack.
What Execution Context is
Global Execution Context
Function Execution Context
Creation Phase
Execution Phase
Lexical Environment
Variable Environment
Scope Chain
this Binding
Execution Context Stack
Before you start a task, your workspace must be prepared. JavaScript does the same thing before it executes code.
When you start a task, the company gives you everything you need: tools, files, access, and context. Only then do you start coding.
Laptop
Runtime tools needed to work
Documents
Variables and function declarations
Credentials
Access to outer scopes
Project files
Code waiting to execute
Team information
Context about this and surrounding environment
Global Execution Context (GEC)
Created when the JavaScript file starts. Created only once for the top-level program.
Function Execution Context (FEC)
Created every time a function is called. Removed when that function finishes.
Eval Execution Context
Created when eval() runs. It is rare and generally discouraged.
This example shows the Global Execution Context, a Function Execution Context, and the final output.
1console.log("Start");23function greet() {4console.log("Hello");5}67greet();89console.log("End");
Final Output
Start
Hello
End
Global Execution Context is created
JavaScript prepares the top-level workspace.
console.log("Start") runs
Output: Start
greet is stored in memory
The function is defined but not executed yet.
greet() is called
A Function Execution Context is pushed.
console.log("Hello") runs
Output: Hello, then greet context is removed.
console.log("End") runs
Output: End
JavaScript scans the code first. var starts as undefined, function declarations are stored completely, and this is prepared.
1console.log(a);23var a = 10;45function sayHi() {6console.log("Hi");7}
No code has run yet during the creation phase. JavaScript has only prepared memory.
During execution, console.log(a) prints undefined. Then a = 10 updates memory to the real value.
Execution Context is easier when you see the full lifecycle as a sequence.
Source Code
Global Execution Context Created
Memory Creation Phase
Execution Phase
Function Called
Function Execution Context Created
Function Finishes
Execution Context Removed
Program Ends
Step through source code, call stack, execution context stack, memory, scope chain, and console output.
1var name = "Zubair";23function greet() {4var age = 30;5console.log(name);6}78greet();
Console
(empty)JavaScript starts by creating the Global Execution Context and reserving memory for global declarations.
Each execution context starts with setup, then runs code.
Creates memory
Stores functions
Stores variables
Initializes var as undefined
Creates scope
Creates this
Runs code
Assigns values
Calls functions
Evaluates expressions
Produces output
In the browser, the global context is connected to the window object. Node.js has a different global object model.
Browser example
In Node.js, the global model is different. Top-level this in a CommonJS module is not the same as browser window. For interviews, remember the concept: every environment still creates a top-level execution context.
A function context has its own memory, arguments, this, and an outer scope reference.
Function Called
Function Context Created
Own Variables
Arguments
this
Outer Scope Reference
JavaScript searches the current scope first, then moves upward. It never searches downward.
The lookup starts in the current scope, then climbs toward the global scope.
1var company = "Google";23function A() {4let team = "Frontend";56function B() {7console.log(company);8console.log(team);9}10}
Looking for
company
Function B
Creation prepares memory; execution updates that memory.
this is created for execution contexts, but its value depends on how code is called.
Global
Browser: window
In browsers, global this points to window.
Function
Depends on call site
Strict mode can make this undefined.
Method
Object before the dot
user.sayHi() sets this to user.
Arrow Function
Outer this
Arrow functions do not create their own this.
Class
Instance
Methods usually use this for the current instance.
Constructor
New object
new User() binds this to the created object.
The call stack grows when functions call functions and shrinks when they return.
Functions are being called, so contexts are being pushed.
Creation Phase prepares the containers. Execution Phase fills and updates them.
Names are prepared during creation and updated during execution.
Function declarations are stored as callable functions.
Each function call receives its own arguments object or parameters.
The value of this is created for each execution context.
Each context keeps a reference to its outer lexical environment.
Output appears during execution, not creation.
A checkout flow is a practical way to see function execution contexts being created and removed.
Imagine a checkout process.
1checkout();
Inside:
1validateCart();23processPayment();45sendEmail();
Checkout context flow
Global | v checkout() | v validateCart() | v Return | v processPayment() | v Return | v sendEmail()
Each function gets its own execution context. After each function finishes, that context is removed from the call stack.
Every execution context stores variable memory, scope access, and this.
Execution context contents
Execution Context
|-- Variable Environment
| (variables & functions)
|
|-- Scope Chain
| (access to outer scopes)
|
|-- this
(current object reference)Global to function context
Global Execution Context
+------------------------------+
| Variables |
| Functions |
| this |
+--------------+---------------+
|
Function Called
v
+------------------------------+
| Function Execution Context |
| Local Variables |
| Arguments |
| Scope Chain |
| this |
+------------------------------+The company opens once like the Global Execution Context. Every task creates a temporary function context.
Company lifecycle
Company Opens
|
v
Global Execution Context
Employee Starts a Task
|
v
Function Execution Context
Employee Finishes Task
|
v
Function Context Destroyed
Company Closes
|
v
Global Context DestroyedThis one concept explains many JavaScript behaviors that appear in interviews and real projects.
Executes code
Stores variables and functions
Manages function calls
Handles scope
Determines the value of this
Supports recursion and nested function calls
An Execution Context is the environment in which JavaScript code is executed. Every execution context has two phases: the Memory Creation Phase, where variables and functions are allocated memory, and the Execution Phase, where code runs line by line. JavaScript creates one Global Execution Context when a program starts and creates a new Function Execution Context each time a function is invoked. These contexts are managed using the Call Stack.
Each example includes run, reset, step-by-step highlighting, copy, console, memory, and explanation.
1var user = "Zubair";2console.log(user);
Click Run to see output.
The global context creates user first, then execution assigns the value and logs it.
Every function creates a context, whether it is rendering UI, handling a request, or processing an event.
Every render calls your component function and creates a fresh execution context.
Each request runs the handler in a new context with request and response references.
The module creates its own top-level environment for exports, imports, and variables.
Callbacks and async handlers create contexts when response data arrives.
Every recursive call adds another execution context to the stack.
A click handler creates a context only when the user triggers the event.
then and async continuations run in their own function execution contexts.
These are the mistakes that make execution context feel harder than it is.
Thinking Execution Context equals Call Stack.
Confusing Scope Chain with Call Stack.
Thinking variables move between contexts.
Misunderstanding this binding.
Ignoring Creation Phase.
Every answer includes a simple explanation, technical explanation, interview answer, real-world example, memory trick, and common mistake.
Simple explanation
It is JavaScript's workspace for running code.
Technical explanation
An execution context stores the variable environment, lexical environment, this binding, and references needed to execute code.
Interview answer
Execution Context is the environment JavaScript creates to execute global code or a function call.
Real-world example
Calling greet() creates a new function execution context.
Memory trick
Context equals workspace.
Common mistake
Confusing the workspace with the stack that stores workspaces.
Fifteen multiple-choice questions with explanations, wrong-answer reasoning, exam tips, and memory tricks.
Progress
0/15 answered
Quick revision for the terms that usually get mixed together.
Understand Creation Phase first.
Remember every function creates a new Execution Context.
Learn Scope Chain before Closures.
Understand this separately.
Never confuse Execution Context with the Call Stack.