The **DOM** is a live, tree-structured representation of an HTML page maintained by the browser. JavaScript can **read** (access content, attributes, styles) and **write** (add/remove/modify elements). The **document** object is the entry point. Every HTML tag becomes a **node** in the tree.
1Understanding What is the DOM
The DOM is a live, tree-structured representation of an HTML page maintained by the browser. JavaScript can read (access content, attributes, styles) and write (add/remove/modify elements). The document object is the entry point. Every HTML tag becomes a node in the tree.
DOM manipulation is relatively slow compared to JavaScript operations. Batch your DOM changes (document fragments, class toggling) to minimize layout recalculation.
// Accessing the DOM
console.log(document.title); // page title
console.log(document.URL); // current URL
console.log(document.body); // <body> element
// Tree navigation
const heading = document.querySelector('h1');
console.log(heading.textContent); // heading text2Practical Example
Here is a real-world application of What is the DOM showing how it is used in production JavaScript code.
// DOM tree structure
const list = document.getElementById('myList');
console.log(list.children.length); // number of <li>
console.log(list.parentElement.id); // parent's id
console.log(list.firstElementChild); // first <li>3Best Practices
Follow these guidelines when working with What is the DOM:
1. Cache DOM references to avoid repeated lookups
2. Use document fragments for batch insertion
3. Prefer classList over manually manipulating className string
Tip: DOM manipulation is relatively slow compared to JavaScript operations. Batch your DOM changes (document fragments, class toggling) to minimize layout recalculation.
// Accessing the DOM
console.log(document.title); // page title
console.log(document.URL); // current URL
console.log(document.body); // <body> element
// Tree navigation
const heading = document.querySelector('h1');
console.log(heading.textContent); // heading text