Start here.
A browser turns HTML into a document object model, usually called the DOM. HTML tags become element nodes in a tree: a heading can be a child of a section, which is a child of the body. JavaScript can read and change this tree through document, an object supplied by the browser. The DOM is not part of JavaScript's core language, so a plain Node.js program has no page document by default.
document.querySelector('h1') searches for the first element matching a CSS selector. The string 'h1' selects a heading tag; '.notice' would select a class. If nothing matches, the result is null. A variable holding an element refers to a live node in the current document. Changing its textContent changes the text the browser shows. Creating a node and appending it places new content into the tree.
A script can only find elements that exist when it runs. A module script normally waits until HTML has been parsed, while a script placed early without that behavior may run before its target appears. When explaining a DOM example, picture the matching HTML beside the code. The selector names a node, the property describes what changes, and the browser renders the updated tree.
Build the model.
The browser parses HTML into a tree of nodes. JavaScript can find and change nodes through the Document Object Model. The DOM is a browser API, and its current state may differ from the original HTML after scripts or user interactions change it.
A selector returns a node, a list of nodes, or nothing, so absence needs handling. Changing textContent treats content as text; assigning innerHTML parses markup and needs care with untrusted data. Most interfaces are easier to reason about when one part of the code owns each update.
How it works.
The browser parses HTML into a document tree. An element node has a tag name, attributes, children, and a place in that tree. document.querySelector searches for the first element matching a CSS selector and returns null if there is no match. Code should account for that possibility, especially when one script is used on more than one page.
Changing textContent replaces the node's text with ordinary text. Changing innerHTML asks the browser to parse a string as markup, so untrusted text can become elements or executable content. Prefer textContent when displaying data. Also think about timing: a script that runs before an element has been parsed may not find it. Loading a script with defer or waiting for DOMContentLoaded can solve that ordering problem.
Read it line by line.
const heading = document.querySelector('h1');
if (heading) heading.textContent = 'Hello';First notice. The null check acknowledges that a matching node may not exist.
The selector asks the document for its first h1. If there is none, heading is null and the if body is skipped. If there is one, assigning textContent changes what the element displays. The assignment does not rewrite the source HTML file; it changes the live DOM held by this browser page.
See the rule again.
const note = document.createElement('p');
note.textContent = '<hello>';
document.body.append(note);The page displays the literal text <hello> inside a paragraph. createElement creates the element, textContent inserts text safely, and append places it in the live document tree. The browser does not treat <hello> as a tag because it was assigned as text.
One thing to remember.
The DOM is a mutable tree supplied by the browser.