← Back to list

Understanding JavaScript Execution Context (The Right Way)

If you’ve ever wondered how JavaScript actually runs your code, everything comes down to one core concept:

Mohd Ainul · 2026-04-22 10:55 · 11 claps · 1.6 min read
#javascript #execution-context #call-stack #javascript-hoisting
Open on Medium ↗
Wiki topics: 🌐 · Web Development

Understanding JavaScript Execution Context (The Right Way)

If you’ve ever wondered how JavaScript actually runs your code, everything comes down to one core concept:

👉 Execution Context

— -

What is Execution Context?

An Execution Context is the environment where JavaScript code is evaluated and executed.

Think of it like a container (or box) where your entire JavaScript program runs.

— -

Components of Execution Context

Every execution context has two phases:

  1. Memory Phase (Creation Phase)

Also called: Variable Environment

In this phase, JavaScript allocates memory for variables and functions.

Variables → assigned undefined Functions → stored with their entire code

— -

  1. Code Execution Phase

Also called: Thread of Execution

In this phase, JavaScript executes code line by line:

  • Assigns actual values to variables
  • Executes functions
  • Performs calculations

— -

Important Rule

JavaScript is:

👉 Single-threaded 👉 Synchronous

Meaning:

  • Executes one operation at a time
  • Follows order (top → bottom)
  • Moves to the next line only after the current one finishes

— -

Example

var n = 2;

function square(num) { var answer = num * num; return answer; }

var result = square(4);

— -

Step-by-Step Execution

Phase 1: Memory Creation

n → undefined square → function code result → undefined

— -

Phase 2: Code Execution

n = 2 result = square(4)

— -

What Happens When a Function is Called?

When a function is invoked:

square(4)

A new Execution Context is created.

— -

Inside Function Execution Context

Memory Phase

num → undefined answer → undefined

— -

Code Execution Phase

num = 4 answer = 16 return 16

After return:

  • Control goes back to the global execution context
  • Function execution context is deleted

— -

Call Stack (Very Important)

JavaScript uses a Call Stack to manage execution contexts.

— -

How it works:

  1. Global Execution Context → pushed
  2. Function call → new context pushed
  3. Function finishes → popped out

— -

Example Flow

[ Global Execution Context ] ↓ [ square() Execution Context ] ↓ (pop after return) ↓ [ Global Execution Context ]

— -

Key Takeaways

  • Everything in JavaScript runs inside an Execution Context
  • It has two phases: — Memory Phase — Code Execution Phase
  • Functions create new execution contexts
  • Call Stack manages execution order
  • JavaScript is single-threaded & synchronous

— -

One-Line Summary

👉 JavaScript first prepares memory, then executes code — managed by the call stack.

— -

Final Thought

Understanding Execution Context helps you master:

  • Hoisting
  • Closures
  • Scope
  • Call Stack behavior

This is the foundation of JavaScript internals.


메타데이터
post_id
acd95c7ba46f
slug
understanding-javascript-execution-context-the-right-way-acd95c7ba46f
url
https://medium.com/@mohdainul01/understanding-javascript-execution-context-the-right-way-acd95c7ba46f
canonical_url
https://medium.com/@mohdainul01/understanding-javascript-execution-context-the-right-way-acd95c7ba46f
author_url
https://medium.com/@mohdainul01
status
ok
fetched_at
2026-07-28 07:38:09