π§ Deep Dive into Execution Context in JavaScript

Understanding how JavaScript works internally is crucial to writing better, more optimized code. One of the foundational concepts behind the scenes is the Execution Context.
In this blog, letβs break it down step by step to truly understand what it is, how it works, and why it matters.
π What is Execution Context?
In JavaScript, an execution context is the environment in which a piece of code runs.
Think of it as a wrapper that helps the JavaScript engine manage and keep track of variables, functions, and the scope in which your code is executing.
π Types of Execution Contexts
There are three types of execution contexts:
Global Execution Context (GEC)
Created by default.
Only one GEC exists at a time.
Binds variables and functions to the global object (
window
in browsers).
Function Execution Context (FEC)
Created each time a function is called.
Has its own scope and variables.
Eval Execution Context (rarely used)
- Created when running code inside
eval()
.
- Created when running code inside
βοΈ Phases of Execution Context
Every execution context goes through two main phases:
1. ποΈ Creation Phase:
Variable Environment is created.
Functions are hoisted.
Variables are hoisted and set to
undefined
.this
is determined.
2. π Execution Phase:
Code is executed line by line.
Variable values are assigned.
Function calls are made.
π§± Call Stack (Execution Stack)
JavaScript uses a call stack to manage execution contexts.
Example:
jsCopyEditfunction greet() {
console.log("Hello!");
}
function welcome() {
greet();
}
welcome();
π¦ Call Stack:
scssCopyEditGlobal β welcome() β greet()
Each function call pushes a new context onto the stack. When done, it's popped off.
π Code Example
jsCopyEditvar x = 10;
function sum() {
var y = 20;
console.log(x + y);
}
sum(); // Output: 30
GEC created β
x
is hoistedFEC for
sum()
created βy
is initializedCode executes β logs
30
β οΈ Important: Hoisting
jsCopyEditconsole.log(a); // undefined
var a = 5;
In the creation phase, a
is hoisted and initialized with undefined
. The actual assignment happens in the execution phase.
π§ Why It Matters
Mastering execution context helps you:
Understand scope
Avoid hoisting pitfalls
Debug effectively
Grasp closures, async/await, and
this
keyword
π― Real Interview Questions
What is an execution context in JavaScript?
How does the call stack work?
Explain hoisting and how it happens during execution.
How is the
this
keyword determined in different contexts?
π οΈ Practice Tip
Use jsTutor to visualize how execution contexts are created and stacked. Itβs a game changer!
π Final Thoughts
Execution Context is one of those core concepts that power everything in JavaScript. It may feel abstract at first, but once you get it, your understanding of the language will be on a whole new level.
βοΈ Written by Krishan β follow me on Twitter and GitHub and Linkedin
π‘ Check out the current code examples and notes repo: GitHub
π Read more on my blog: Hashnode
Subscribe to my newsletter
Read articles from krishan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

krishan
krishan
π Hey, I'm a Frontend Developer passionate about building clean, user-friendly interfaces. π Learning and sharing everything from React, JavaScript, HTML/CSS to advanced topics like Data Structures, Algorithms & System Design. π» Documenting my journey from fundamentals to becoming a top-tier developer β one blog at a time. π Join me as I break down complex topics into easy, practical lessons β with GitHub repos, code snippets, and real-world examples. π Consistent growth, community learning, and aiming high!