How JavaScript Works in Browser0%

How JavaScript Works in Browser

Beginner12 min read•Updated: Jul 11, 2026
Study Materials

How JavaScript Works in Browser

To write clean, bug-free JavaScript, you need a solid mental model of what happens behind the scenes when a browser downloads, parses, and executes your code.


1. The Browser Execution Architecture

Every modern web browser contains two primary components for web pages:

  1. 1
    The Rendering Engine (e.g. Blink in Chrome, Gecko in Firefox, WebKit in Safari): Parses HTML into the DOM (Document Object Model) and CSS into the CSSOM (CSS Object Model).
  2. 2
    The JavaScript Engine (e.g. V8 in Chrome & Node.js, SpiderMonkey in Firefox, JavaScriptCore in Safari): Reads, compiles, and executes JavaScript code.
Visual Architecture Blueprint
+-----------------------------------------------------------------------------+
|                          JAVASCRIPT RUNTIME ENVIRONMENT                     |
+-----------------------------------------------------------------------------+
|   [ Call Stack ]                      [ Memory Heap ]                       |
|   Tracks function execution           Stores variables, objects, &          |
|   frames (LIFO)                       allocated memory data                 |
+-----------------------------------------------------------------------------+
|   [ Web APIs ] (Provided by the Browser, NOT JavaScript itself!)            |
|   DOM Events, setTimeout(), fetch(), LocalStorage, console.log              |
+-----------------------------------------------------------------------------+
|   [ Callback Queue ]                  [ Event Loop ]                        |
|   Holds finished async tasks          Continuously checks if Call Stack is  |
|   ready to execute                    empty; pushes callbacks to Stack!     |
+-----------------------------------------------------------------------------+

2. The Three Pillars of the JS Runtime

1. The Call Stack

JavaScript is a single-threaded programming language, meaning it has only one call stack and can execute only one task at a time.

  • When you invoke a function, a new execution frame is pushed onto the stack.
  • When the function finishes and returns, its frame is popped off the top of the stack.

2. The Memory Heap

An unstructured pool of computer RAM where objects, arrays, and functions are allocated and stored. Unused memory is automatically freed by the engine's Garbage Collector.

3. The Event Loop & Web APIs

If JavaScript is single-threaded, how can it fetch data from an API without freezing the entire browser screen?

  • Heavy asynchronous tasks (fetch, setTimeout, DOM click events) are delegated to the browser's Web APIs.
  • When the timer or network request completes, the Web API pushes the callback into the Callback Queue.
  • The Event Loop constantly monitors: "Is the Call Stack currently empty?" As soon as the call stack is clear, the Event Loop takes the first task from the queue and pushes it onto the Call Stack!

3. How JavaScript is Linked to HTML Documents

There are three ways to embed JavaScript into an HTML page:

Separate your logic into a .js file:

HTML
<!-- In index.html -->
<script src="app.js"></script>

B. Internal Script Tag

Inside an HTML <script> block:

HTML
<script>
console.log("Inline script executed!");
</script>

C. Inline HTML Event Attribute (Deprecated / Avoid)

HTML
<button onclick="alert('Clicked!')">Click Me</button>

4. Modern Script Loading: defer vs async

Where should you place <script> tags in your HTML?

Output
Parsing HTML: =====[ Script Download ]======[ Script Execution ]======> HTML resumes
(Without defer, HTML parsing is paused while JS downloads!)

The Modern Solution: Use defer in the <head>!

HTML
<head>
<script src="app.js" defer></script>
</head>
  • defer: Downloads JavaScript in the background while HTML parsing continues. Executes scripts only after the full HTML document is parsed, preserving script execution order.
  • async: Downloads in the background, but interrupts HTML parsing and executes immediately as soon as download completes (great for independent analytics scripts like Google Analytics).

Interactive Quiz

Practice Quiz

Question 1
Is JavaScript a single-threaded or multi-threaded language by default?
Question 2
What is the primary role of the JavaScript Event Loop?
Question 3
Where are functions and execution contexts pushed and popped in JavaScript?
Question 4
Why is adding the defer attribute to external <script> tags in <head> considered best practice?
Question 5
Which of the following features is provided by the Browser Web APIs rather than core ECMAScript?
Next Lesson

Setting up Environment: VSCode, Live Server

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext