Ticker

6/recent/ticker-posts

Chapter 2: How Browsers Work and Where JavaScript Actually Runs : whosgeek


Chapter 2 How Browsers Work and Where JavaScript Actually Runs  whosgeek


Before Learning JavaScript, Understand the Browser

Many beginners start learning JavaScript immediately.

They learn:

  • Variables

  • Functions

  • Loops

  • Arrays

without understanding where JavaScript actually runs.

This is similar to learning how to drive a car without understanding the dashboard.

You may be able to drive, but you won't fully understand what's happening.

To become a strong JavaScript developer, you must first understand the environment where JavaScript executes.

That environment is the browser.


Imagine the Browser as a Restaurant

Let's continue our restaurant analogy.

Imagine you visit a modern restaurant.

The restaurant has different departments.

Building Structure

Responsible for:

  • Walls

  • Tables

  • Doors

  • Kitchen Layout

In web development, this is:

HTML


Interior Decoration

Responsible for:

  • Colors

  • Lighting

  • Furniture Design

In web development, this is:

CSS

Staff and Operations

Responsible for:

  • Taking Orders

  • Serving Food

  • Handling Requests

In web development, this is:

JavaScript

Without staff, the restaurant cannot function.

Without JavaScript, modern websites cannot provide interactive experiences.

What Happens When You Open a Website?

Suppose you visit:

www.example.com

Several things happen in milliseconds.


Step 1: Browser Sends Request

The browser contacts the web server.

Request:

Can you send me this webpage?

Step 2: Server Sends Files

The server responds with:

HTML
CSS
JavaScript
Images
Fonts
Videos

Step 3: Browser Reads HTML

Example:

<h1>Welcome</h1>

<button>
   Click Me
</button>

The browser begins constructing the page.


Step 4: Browser Applies CSS

Example:

h1{
   color:red;
}

The browser applies styling.


Step 5: Browser Executes JavaScript

Example:

document
  .querySelector("button")
  .addEventListener(
      "click",
      ()=>{
         alert("Hello");
      }
  );

Now the page becomes interactive.


The Browser Is More Than a Viewer

Many people think a browser simply displays websites.

Modern browsers are incredibly complex software systems.

Popular browsers include:

  • Chrome

  • Firefox

  • Edge

  • Safari

Each browser contains:

Rendering Engine

JavaScript Engine

Network Layer

Storage System

Security Layer

These components work together continuously.


What Is a JavaScript Engine?

A JavaScript engine is software responsible for executing JavaScript code.

Different browsers use different engines.

Chrome

Uses:

V8 Engine

Firefox

Uses:

SpiderMonkey

Safari

Uses:

JavaScriptCore

Edge

Uses:

V8 Engine

Why V8 Is Important

The V8 engine changed JavaScript forever.

Before V8:

JavaScript was slower

After V8:

JavaScript became extremely fast

Google invested heavily in performance optimization.

V8 converts JavaScript into machine code.

This allows programs to run much faster.


Understanding the DOM

One of the most important browser concepts is:

DOM

which stands for:

Document Object Model

The DOM is a representation of the webpage.

Example HTML:

<body>

<h1>Hello</h1>

<button>
Click
</button>

</body>

The browser converts it into:

Document
   │
   └── Body
         │
         ├── H1
         │
         └── Button

This tree structure becomes the DOM.


Why the DOM Matters

JavaScript communicates with webpages through the DOM.

Example:

document
  .querySelector("h1")
  .textContent =
    "Welcome";

JavaScript changes the webpage instantly.

Without the DOM, JavaScript would have no way to interact with HTML elements.


Browser APIs

Many developers assume everything belongs to JavaScript.

Not true.

JavaScript itself is actually quite small.

Features such as:

setTimeout()

fetch()

localStorage()

navigator

geolocation

are provided by the browser.

These are called:

Browser APIs

Real World Example

Consider:

fetch(
   "https://api.example.com"
);

Many beginners think JavaScript performs the network request.

Actually:

JavaScript
      ↓
Browser API
      ↓
Network Request
      ↓
Response Returned

The browser handles the heavy work.

JavaScript simply receives the result.


JavaScript Is Single Threaded

This statement confuses many developers.

JavaScript executes code using:

One Main Thread

Example:

console.log("A");

console.log("B");

console.log("C");

Output:

A
B
C

One operation at a time.


Then How Can Websites Do Many Things?

Imagine using YouTube.

While watching a video:

  • Comments load

  • Notifications appear

  • Recommendations update

  • Messages arrive

Everything seems simultaneous.

The secret lies in:

Browser APIs

Event Loop

Callback Queue

We will study these in later chapters.

These components allow JavaScript to remain responsive.


Browser Storage

Browsers can store data locally.

Examples:

Local Storage

localStorage.setItem(
   "name",
   "Aqad"
);

Data remains available even after refreshing the page.


Session Storage

sessionStorage.setItem(
   "theme",
   "dark"
);

Data exists only during the current session.


Cookies

Often used for:

  • Authentication

  • User Preferences

  • Tracking

Example:

document.cookie =
   "username=Aqad";

Browser Security

Browsers protect users through security policies.

One important rule:

Same Origin Policy

This prevents malicious websites from accessing data belonging to another website.

For example:

evilsite.com

cannot freely access information from:

gmail.com

This protection is essential for internet security.


The Journey of a Button Click

Let's understand what happens internally.

Example:

<button>
Buy Now
</button>

JavaScript:

button.addEventListener(
   "click",
   ()=>{
      console.log(
         "Order Placed"
      );
   }
);

User clicks button.

Flow:

User Clicks
      ↓
Browser Detects Event
      ↓
Event Listener Triggered
      ↓
JavaScript Executes
      ↓
Action Performed

All of this happens almost instantly.


Why Understanding Browsers Matters

Many developers memorize JavaScript syntax.

Few understand the environment where JavaScript runs.

When debugging issues involving:

  • API Calls

  • DOM Manipulation

  • Performance Problems

  • Event Handling

browser knowledge becomes extremely valuable.

Professional developers understand both:

JavaScript

+

Browser Internals

This combination makes troubleshooting much easier.


Key Takeaways

Remember these points:

  • Browsers execute JavaScript.

  • JavaScript engines run JavaScript code.

  • Chrome uses the V8 engine.

  • The DOM represents webpage structure.

  • Browser APIs provide extra functionality.

  • JavaScript is single-threaded.

  • Browsers provide storage mechanisms.

  • Security policies protect users.

These concepts form the foundation for understanding advanced topics such as:

  • Execution Context

  • Call Stack

  • Event Loop

  • Promises

  • Async/Await


Conclusion

A browser is much more than a tool for viewing websites. It contains rendering engines, JavaScript engines, storage systems, networking capabilities, and security layers that work together to create modern web experiences.

Understanding how browsers work gives you a major advantage as a JavaScript developer because it helps explain why JavaScript behaves the way it does.

In the next chapter, we will start writing actual JavaScript code and learn about variables, data types, and how JavaScript stores information in memory.

Post a Comment

0 Comments