JavaScript Engine Concepts Complete Guide
JavaScript Engine Concepts Complete Guide
JS
JS Engine
Concepts Guide
Master Call Stack, Event Loop, Memory Heap, and Execution
Context. Understand how JavaScript really works under the
hood.
16 4 12+
Pages Core Topics Examples
JS
This guide covers the internal workings of JavaScript engines. Understanding these concepts is crucial for writing performant
code and acing technical interviews.
💡 Interview Tip
Understanding Event Loop priority is crucial! Focus on: Sync → Microtasks → Macrotasks
JavaScript code doesn't run by magic. A JavaScript Engine (like V8 in Chrome/[Link]) handles everything. Let's visualize
the complete runtime architecture.
JS
🔄 EVENT LOOP
Coordinator between stack & task queues
The heap is an unstructured region of memory where objects, arrays, and functions are stored. Primitives are stored in the
stack (fast, small size).
JS
Where Each Data Type Lives
[Link]
x = 42
name = "Bob"
flag = true
Stack: Holds references (memory addresses) to objects in the heap Heap: Actual data lives here. Garbage Collector cleans unused
memory
JS
[Link]
🗑️ Key Point: When an object has no references pointing to it, it becomes "unreachable" and the Garbage Collector
automatically frees that memory. You don't need to manually free memory in JavaScript!
⚠️ Memory Leak: If you keep references to objects you no longer need, they can't be garbage collected. Always clean up
event listeners and large data structures when done!
An Execution Context is the environment in which JavaScript code is evaluated and executed. It contains variables, scope
chain, and the this keyword.
JS
🌍 📦 ⚠️
Global EC Function EC Eval EC
Created when script starts. Only ONE per Created every time a function is CALLED. Created inside eval(). Avoid using eval() —
program. Creates window or global. Each call gets its OWN context. security risk!
📁 🔗 👆
Variable Environment Scope Chain `this` Keyword
Variables, functions, arguments Current scope + all parent scopes Depends on HOW function is called
JS
[Link]
var a = 10;
function outer() {
var b = 20;
function inner() {
var c = 30;
[Link](a + b + c); // 60
}
inner();
}
outer();
inner() called
📚 Key: Each function call creates a new EC pushed onto the stack. When a function finishes, its EC is destroyed and popped
off. Inner functions can access outer variables via the scope chain.
JS
The Call Stack is a LIFO (Last In, First Out) data structure that tracks which function is currently being executed. JavaScript
is single-threaded — one call stack, one thing at a time!
[Link]
Peak! 🔝
multiply returns
JS
✅ Single-threaded ONE call stack → ONE thing at a time
Stack Overflow
[Link]
oops() 💥 OVERFLOW
oops()
main()
🚨 The Blocking Problem: A long synchronous task FREEZES the entire UI. This is exactly why we need the Event Loop and
async operations!
The Event Loop is the mechanism that allows JavaScript to perform non-blocking operations despite being single-
threaded. Here's the complete picture of how everything connects.
JS
JAVASCRIPT RUNTIME
▲
when done, pushes
callback to a queue
The Event Loop is the mechanism that allows JavaScript to perform non-blocking operations despite being single-
threaded. It coordinates between the Call Stack and Task Queues.
JS
Is Stack Empty?
2 If not empty, keep executing. If empty, proceed.
Repeat Forever
6 Go back to step 1 ♻️
[Link]("1: Start");
{}
Topic 4 - Part 3
JS
[Link]
OUTPUT:
1: Start → 3: End → 2: Timeout
🔍 Step-by-Step Execution
STEP 1 [Link]("1: Start") → Output: "1: Start"
STEP 2 setTimeout → Callback sent to Web API timer (0ms) → Moves to Macrotask Queue
STEP 4 Stack empty → Event Loop picks macrotask → Output: "2: Timeout"
Microtasks (empty)
JS
[Link]
setTimeout(() => {
[Link]("2: setTimeout"); // MACROTASK
}, 0);
[Link]().then(() => {
[Link]("3: Promise"); // MICROTASK
});
OUTPUT:
1: Script start
4: Script end
3: Promise ← microtask runs FIRST
2: setTimeout ← macrotask runs AFTER
⚡ Critical Rule: ALL microtasks are drained BEFORE any macrotask runs. Promises always beat setTimeout!
[Link]("1");
setTimeout(() => [Link]("2"), 0);
{}
Topic 4 - Part 5
JS
[Link]
[Link]()
.then(() => {
[Link]("3");
setTimeout(() => [Link]("4"), 0);
})
.then(() => [Link]("5"));
setTimeout(() => [Link]("6"), 0);
[Link]("7");
✅ FINAL OUTPUT:
1, 7, 3, 5, 2, 6, 4
JS
MICRO 1 Execute cb2: [Link]("3") → Output: "3"
Inside cb2: setTimeout → cb4 goes to Macrotask Queue
.then() chains → cb5 goes to Microtask Queue
📝 Summary: Sync (1, 7) → Drain Microtasks (3, 5) → Macrotask (2) → Macrotask (6) → Macrotask (4). Note how "4" was added
during microtask execution but still runs last!
Key Takeaways
JS
[Link]
await [Link]();
// ↑ everything AFTER await goes to MICROTASK queue
[Link]("2: after await"); // microtask
}
[Link]("3: script start");
asyncFunc();
[Link]("4: script end");
OUTPUT:
3: script start
1: async start ← runs synchronously until await
4: script end
2: after await ← microtask, runs after stack is empty
⚡ Remember: await splits the function — everything BEFORE await runs synchronously. Everything AFTER await becomes a
microtask!
[Link]("Start");
[Link]().then(() => {
[Link]("Microtask 1");
[Link]().then(() => [Link]("Microtask 2"));
});
setTimeout(() => [Link]("Macrotask 1"), 0);
[Link]("End");
OUTPUT:
Start → End → Microtask 1 → Microtask 2 → Macrotask 1
🔑 Critical: Nested microtasks are also drained before any macrotask. The queue must be completely empty!
Execution Priority
{}
Reference
JS
🥇 1 Synchronous Code
[Link], assignments, function calls — runs FIRST, completely
🥈 2 Microtasks
[Link]/catch/finally, async/await (after await), queueMicrotask()
🥉 3 Macrotasks
setTimeout, setInterval, setImmediate, I/O operations, UI rendering
Memory Heap Unstructured memory storage Objects, arrays, functions stored here
Call Stack LIFO stack of function calls One thing at a time (single-threaded)
Execution Context Environment for code execution Created per function call
JS
1 JavaScript is single-threaded — one call stack, one thing at a time
2 Synchronous code always runs first, completely, before any async code
4 Event loop drains ALL microtasks before picking the next macrotask
🎯 Interview Tip: When asked about execution order, always trace: 1) All sync code first, 2) All microtasks (drain completely),
3) One macrotask, then repeat!
JS
Thank You!
You've Mastered JS Engine Concepts
You now understand how JavaScript really works under the
hood! Use this knowledge to write better code and ace your
technical interviews.
✅ Memory Heap ✅ Execution Context ✅ Call Stack ✅ Event Loop ✅ Task Queues
16 4 ∞
Pages Completed Core Topics Potential Unlocked