How JavaScript Works Behind the Scenes – A Beginner’s Guide

Search for a command to run...

No comments yet. Be the first to comment.
Computers have been helping us solve problems more quickly and intelligently for decades, but even the supercomputers have their limitations. Quantum computing is a new and exciting technology that has the potential to completely change the rules of ...

Introduction: The Security Issue You Can't Afford to Overlook If you're still sharing the same password across your email, bank, and Netflix account, or if your password is a combination of "Password123," then your security is gravely compromised. W...

Artificial Intelligence (AI) has already been part of our everyday life. From virtual assistants to automated recommendation systems, AI uses various technologies that we use regularly. AI tools like ChatGPT, Google Gemini, and GitHub Copilot have ch...

Building a quantum computer isn’t about power — it’s about survival. 🌪️ The Fragile Nature of Quantum Computers Quantum computers operate on qubits, which can exist in a superposition of 0 and 1.But this power comes with a cost — qubits are incredi...

Technology is progressing at a rapid rate, and so is its energy consumption. From massive AI models running in data centers to billions of IoT devices always online, the IT industry is now one of the largest contributors to global electricity demand....

The web's language is called JavaScript. Everything from dynamic content to sophisticated web applications is powered by it. However, have you ever pondered the inner workings of JavaScript? Why is it so quick, asynchronous, and single-threaded?
We'll reveal everything in this blog, from the engine to the event loop, in the most straightforward manner imaginable.
JavaScript is a single-threaded, interpreted, high-level programming language. It enables websites to become interactive and operates within your browser, such as Chrome or Firefox. Consider JavaScript magic in the form of sliders, pop-ups, animations, and form validations.
A program that runs JavaScript code is called a JavaScript engine.
Each browser has a unique engine.
V8 (Node.js, Google Chrome)
Firefox's SpiderMonkey
Safari's JavaScriptCore
Chakra (Microsoft Edge Legacy)
Since V8 is the most well-liked and frequently utilized, we'll concentrate on it.
V8 Engine Components:
1.Memory Heap
Where memory allocation takes place.
And holds the definitions of functions, variables, and objects.
2.The Call Stack
Controls the invocation of functions.
Adheres to the Last-In-First-Out (LIFO) principle.
Suppose you write the following code:
function greet() {
console.log("Hi!");
}
greet();
Behind-the-scenes activities:
The code is parsed by the JavaScript engine. It creates a Global Execution Context.
Memory is set aside for functions and variables.
The Call Stack receives a push of greet().
Console.log("Hi!") is pushed and run.
When a function is finished, it is removed from the stack.
An Execution Context is created by JavaScript when code is executed, and it includes:
The environment in which variables and functions reside is known as the variable environment.
For addressing variable access, use the scope chain.
This depends on how a function is called.
Execution Context Types:
It has two types.
1.When the script starts, the global execution context is created.
2.Every time a function is called, a functional execution context is created.
Memory Heap:
Garbage Collection:
JavaScript manages memory automatically.
Memory used by items that are no longer in use is released by the garbage collector.
V8 cleans up unused memory using the Mark-and-Sweep Algorithm.
JavaScript manages memory automatically. Memory used by items that are no longer in use is released by the garbage collector.V8 cleans up unused memory using the Mark-and-Sweep Algorithm. it's nature Because JavaScript is single-threaded, it can only perform one action at a time. The Call Stack is used to run all code on a single main thread.
However, how does JavaScript manage asynchronous processes like setTimeout, DOM events, and API calls if it is only single-threaded?
Let's examine that in the following section.
The browser environment's Web APIs, which are not a component of JavaScript itself, are accessible to JavaScript in the browser.
Web examples:
setTimeout()
DOM manipulation
fetch()
Event listeners
To prevent the main thread from being blocked, these tasks are assigned to the browser.
Web APIs pass the callback function to the Callback Queue (also called the Task Queue) when their task is finished, such as after two seconds in setTimeout().
The Call Stack and the Callback Queue are continuously observed by the Event Loop.
How it operates:
The Event Loop pushes the first callback from the Callback Queue to the Call Stack so that it can be executed if the Call Stack is empty.
Example:
console.log("Start");
setTimeout(() => {
console.log("Inside Timeout");
}, 1000);
console.log("End");
Output:
Start
End
Inside Timeout
Why?
The Web API handles setTimeout() → Callback goes to the Callback Queue after 1000 ms, and the Event Loop only sends it to the Call Stack when it is empty.
There are two kinds of task queues in JavaScript:
Macrotask Queue: for setTimeout, setInterval, setImmediate, and I/O tasks.
Microtask Queue: for MutationObserver, queueMicrotask, and Promises.
Prioritized and completed immediately following the current task, microtasks come before macrotasks.
Your Code
↓
JavaScript Engine (Parsing)
↓
Memory Allocation → Execution Context Created
↓
Call Stack (Function Execution)
↓
Web APIs (for async tasks)
↓
Callback Queue (waiting)
↓
Event Loop (checks if Call Stack is empty)
↓
Callback pushed to Call Stack → Executed
Although JavaScript appears to be a straightforward scripting language, it is actually a sophisticated, well-coordinated system that uses the JavaScript engine, event loop, call stack, and browser APIs to manage both synchronous and asynchronous tasks.
Debugging, performance optimization , and writing better code all depend on an understanding of this inner working.