DEV Community

Himanshu Gupta
Himanshu Gupta

Posted on • Edited on

How JavaScript Works & Execution Context | EP-01

๐Ÿง  What Is Execution Context? (Simple Words)

Think of the execution context like a classroom where JavaScript works.

In this classroom:

There is a whiteboard where all important notes (variables & functions) are written โ†’ Memory Component

There is a teacher who reads and executes each instruction one-by-one โ†’ Thread of Execution

๐Ÿ“Œ 1. Memory Component (Variable Environment)
๐Ÿง  Simple Meaning:

A place where JavaScript stores data before running the code.

๐ŸŽ’ Real-Life Example:

Imagine you enter a classroom and the teacher writes names on the board:

studentName = "Ali"

age = 10

greet = function(){...}

The whiteboard is the memory component, storing data before the lesson starts.

๐Ÿ“Œ 2. Code Component (Thread of Execution)
๐Ÿง  Simple Meaning:

The part where JavaScript executes code line by line, like a teacher reading instructions one after another.

๐ŸŽ’ Real-Life Example:

After writing on the whiteboard (memory),
the teacher starts reading the notebook:

Say hello to the students

Explain the lesson

Ask a question

Wait for an answer

Move to next instruction

The teacher cannot read two lines at the same time.

That is the thread of execution.

๐Ÿ“Œ 3. JavaScript Is Synchronous & Single-Threaded
๐Ÿง  Simple Meaning:

JavaScript does ONE thing at a time and in order.

๐ŸŽ’ Real-Life Example:

Imagine a student reading a book aloud.

They must finish one sentence before starting the next.

They cannot read two sentences at the same time.

Thatโ€™s how JavaScript worksโ€”step-by-step, one-by-one.

๐Ÿ’ก Full Real-Life Example Putting It All Together
`Code:
var name = "Sara";
function sayHi() {
console.log("Hi " + name);
}

sayHi();`

๐Ÿง  Real-Life Breakdown:

Memory Component (Whiteboard)

name: "Sara"

sayHi: function

Thread of Execution (Teacher reading steps)

Goes to sayHi()

Executes it โ†’ prints "Hi Sara"

JavaScript finishes this before moving to the next line.

Top comments (0)