JavaScript Working
- This lesson explains the internal working mechanism of JavaScript.
Introduction
To become strong in JavaScript, it is not enough to only write code.
You must also understand how JavaScript works internally.This lesson explains:
How JavaScript code is executed
What is a JavaScript Engine
What is an Execution Context
How JavaScript runs code step by step
Understanding this topic is very important for interviews and advanced concepts like:
Hoisting
Scope
Closures
Call Stack
Asynchronous JavaScript
How JavaScript Works
JavaScript follows a specific execution process:
JavaScript code is written by the developer
JavaScript Engine reads the code
Execution Context is created
Code is executed line by line
Output is produced
JavaScript is:
Single-threaded
Synchronous (by default)
Interpreted (runs line by line)
What is a JavaScript Engine ?
A JavaScript Engine is a program that:
Reads JavaScript code
Converts it into machine-level instructions
Executes the code
The JavaScript engine is the brain that runs JavaScript code.
Popular JavaScript Engines
Different browsers use different JavaScript engines:
Browser JavaScript Engine Google Chrome V8 Mozilla Firefox SpiderMonkey Microsoft Edge V8 Safari JavaScriptCore Why JavaScript Engine is Needed
Computers do not understand JavaScript directly.
They only understand machine code (0s and 1s).So the JavaScript engine:
Reads JavaScript code
Translates it
Executes it
Without a JavaScript engine, JavaScript cannot run.
Internal Working of JavaScript Engine
The JavaScript Engine works in two main phases:
Memory Creation Phase
Execution Phase
These phases happen inside something called Execution Context.
What is an Execution Context ?
An Execution Context is an environment where JavaScript code is executed.
It contains:
Variables
Functions
Scope information
Reference to this
In simple words:
Execution Context decides how and where the code will run.Types of Execution Context
JavaScript has three types of execution contexts:
Global Execution Context (GEC)
Function Execution Context (FEC)
Eval Execution Context (rarely used)
Global Execution Context (GEC)
This is the default execution context.
Created when the program starts
Only one global execution context exists
Code not inside any function runs here
Example
var a = 10;
console.log(a);
This code runs inside the Global Execution Context.
What Happens in Global Execution Context ?
The Global Execution Context has two phases:
Phase 1: Memory Creation Phase
Variables are allocated memory
Functions are stored fully
Variables are initialized as undefined
Phase 2: Execution Phases
Code is executed line by line
Values are assigned
Functions are called
Memory Creation Phase
Example:
var x = 5;
var y = 10;
During memory creation:
x → undefined
y → undefined
No values are assigned yet.
Execution Phase
During execution:
x = 5
y = 10
Now variables hold actual values.
Example: Complete Flow
var a = 10;
var b = 20;
console.log(a + b);
Step 1: Memory Creation
a → undefined
b → undefined
Step 2: Execution
a = 10
b = 20
console.log(30)
Function Execution Context (FEC)
Whenever a function is called:
A new execution context is created
It has its own memory and execution phases
Understanding Function Execution Context (FEC)
Shows how a new execution context is created when a function is called.
function add(x, y) {
var result = x + y;
return result;
}
add(5, 10);
How This Code Executes
Global Execution Context is created
Function definition is stored in memory
When add(5, 10) is called:
A new Function Execution Context is created
Parameters and variables get memory
Code inside function executes
Result is returned
Function execution context is destroyed
Function Execution Context Phases
Just like GEC, FEC also has:
Memory Creation Phase
Execution Phase
Execution Context Stack (Call Stack)
JavaScript manages execution contexts using a stack called the Call Stack.
Rules:
Last In, First Out (LIFO)
The currently executing context stays on top
Understanding Call Stack
Demonstrates how JavaScript uses a stack to manage function execution order.
function first() {
second();
}
function second() {
console.log("Hello");
}
first();
Call Stack Flow
Global Execution Context pushed
first() pushed
second() pushed
console.log() executes
second() popped
first() popped
Global context remains
Why Execution Context is Important
Understanding execution context helps to understand:
Hoisting
Scope chain
Closures
Call stack
Error handling
Many JavaScript interview questions are based on this topic.
Common Mistakes
Thinking JavaScript runs top to bottom only
Ignoring memory creation phase
Not understanding function execution
Confusing execution context with scope