DEV Community

Cover image for DOM in JavaScript — What Actually Happens When Your Code Runs
Kathirvel S
Kathirvel S

Posted on

DOM in JavaScript — What Actually Happens When Your Code Runs

You’ve probably written DOM code like this before:

document.getElementById("title").textContent = "Hello World";
Enter fullscreen mode Exit fullscreen mode

And it works.

But if I ask you:

“What exactly just happened inside the browser?”

Most people pause there.

Let’s fix that — step by step, no skipping, no guessing.


Start With This Simple HTML

<div id="app">
  <p id="text">Loading...</p>
  <button id="btn">Update</button>
</div>
Enter fullscreen mode Exit fullscreen mode

Clean. Nothing fancy.


Now Add JavaScript

const text = document.getElementById("text");
const btn = document.getElementById("btn");

btn.addEventListener("click", () => {
  text.textContent = "Data loaded";
});
Enter fullscreen mode Exit fullscreen mode

Looks simple.

But under the hood, a lot is happening.

Let’s slow it down.


Step 1: Browser Builds the DOM (Before Your JS Runs)

The moment the page loads:

  1. Browser reads your HTML
  2. Parses it line by line
  3. Converts it into a DOM tree

Internally, it looks something like:

div#app
 ├── p#text ("Loading...")
 └── button#btn ("Update")
Enter fullscreen mode Exit fullscreen mode

Important:

This structure lives in memory, not in your .html file.


Step 2: JavaScript Starts Running

Now your script executes:

const text = document.getElementById("text");
Enter fullscreen mode Exit fullscreen mode

What happens internally?

  • The browser goes through the DOM tree
  • Looks for a node with id="text"
  • Finds the <p> element
  • Returns a reference to that node

So now:

text → points to <p> node in memory
Enter fullscreen mode Exit fullscreen mode

Not a copy. The real node.


Step 3: Another Lookup Happens

const btn = document.getElementById("btn");
Enter fullscreen mode Exit fullscreen mode

Same process:

btn → points to <button> node
Enter fullscreen mode Exit fullscreen mode

At this point, your variables are directly connected to DOM nodes.


Step 4: Event Listener Gets Registered

btn.addEventListener("click", () => {
  text.textContent = "Data loaded";
});
Enter fullscreen mode Exit fullscreen mode

Here’s what the browser does:

  • Attaches a “click listener” to the button node
  • Stores your callback function internally
  • Waits

Nothing changes visually yet.


Step 5: User Clicks the Button

Now the real flow begins.

When the user clicks:

  1. Browser detects the click event
  2. It checks the target node (button)
  3. Finds your registered listener
  4. Executes your function

Step 6: You Modify the DOM

text.textContent = "Data loaded";
Enter fullscreen mode Exit fullscreen mode

Internally:

  • The <p> node’s text value is updated
  • The DOM tree is now different in memory

Before:

p → "Loading..."
Enter fullscreen mode Exit fullscreen mode

After:

p → "Data loaded"
Enter fullscreen mode Exit fullscreen mode

Step 7: Browser Re-Renders the Change

The browser notices:

“Something in the DOM changed.”

So it:

  • Updates only that part of the UI
  • Does not reload the page
  • Does not reprocess all HTML

Just a small, efficient update.


Now Let’s Push It a Bit Further

What if you didn’t just update text — but created something new?

btn.addEventListener("click", () => {
  const newMsg = document.createElement("p");
  newMsg.textContent = "New message added";

  document.getElementById("app").appendChild(newMsg);
});
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Again (Don’t Rush This)

1. createElement

  • A new <p> node is created
  • It exists only in memory
  • Not visible yet

2. textContent

  • Adds text inside that node
  • Still not visible

3. appendChild

  • The node is attached to div#app
  • Now it becomes part of the DOM tree

Before:

div
 ├── p
 └── button
Enter fullscreen mode Exit fullscreen mode

After:

div
 ├── p
 ├── button
 └── p (new one)
Enter fullscreen mode Exit fullscreen mode

4. Browser Updates the UI

Now the browser renders the new structure.

You see the new paragraph instantly.


The Pattern You Should Notice

Every DOM operation follows this pattern:

  1. Find or create a node
  2. Modify it
  3. Attach it (if needed)
  4. Browser updates the UI

That’s it.


One Thing You Should Stop Doing

Don’t think:

“I’m changing HTML”

You are not.

You are:

“Updating objects in the DOM tree”

The browser just reflects those updates visually.


Final Thought

If your UI changes, it’s because:

  • A DOM node changed
  • Or a new node was added/removed

There’s no hidden magic.

Just a structured tree in memory… and you controlling it.

Once you start thinking like this, DOM stops being confusing — and starts becoming predictable.

Top comments (0)