DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • Semantic HTML
  • Accessibility (a11y)
  • Forms & Validation
  • Document Metadata & The <head>
  • The Document Object Model (DOM)
  • Character Encoding & Entities
  • Data Attributes & Custom Data
  • Template, Slot & Web Components
  • Responsive Images: Picture, Source & srcset
  • Loading Strategies & Resource Hints
  • The Native Dialog Element
  • Audio, Video & Media APIs
  • SVG Integration & Canvas Basics
  • Advanced Input Types & Datalist
  • Details, Summary, Progress & Meter
  • Global Attributes & Iframe Security
  • Microdata, Schema.org & Structured Data
  • Open Graph & Social Meta Tags
  • Security Headers & CSP in HTML
  • Link Relations & Canonical Tags
← From Beginner to Expert in Fundamentals of Frontend

The Document Object Model (DOM)

18 min read
DOMDOM TreeNode TypesDOM APITraversalManipulation

The DOM: Your HTML as a Living Object

When the browser loads HTML, it creates a tree of objects called the Document Object Model. Every HTML element becomes a node you can read, modify, and delete with JavaScript.

Node Types

TypeValueExample
Element Node1<div>, <p>
Text Node3Text inside elements
Comment Node8<!-- comment -->
Document Node9The root document

DOM Traversal

  • Parent: element.parentNode / element.parentElement
  • Children: element.children (elements only) vs element.childNodes (all nodes)
  • Siblings: element.nextElementSibling / element.previousElementSibling

Selection Methods Performance

MethodReturnsLive?Speed
getElementByIdSingle elementN/AFastest
querySelectorFirst matchNoFast
querySelectorAllNodeListNo (static)Fast
getElementsByClassNameHTMLCollectionYes (live)Fast
getElementsByTagNameHTMLCollectionYes (live)Fast

Live vs Static Collections

A live HTMLCollection updates automatically when the DOM changes. A static NodeList (from querySelectorAll) is a snapshot. Modifying the DOM while iterating a live collection causes bugs!

DocumentFragment for Batch Updates

Instead of appending 100 elements one by one (100 reflows), create them inside a DocumentFragment (0 reflows) and append the fragment once (1 reflow).

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.