What is the DOM in JavaScript?

Answer

The DOM (Document Object Model) is a programming interface for HTML and XML documents. It represents the document as a tree of objects (nodes) where each element, attribute, and text is a node. JavaScript can access and manipulate this tree to dynamically change content, structure, and styles. Common operations: document.getElementById("id"), document.querySelector(".class"), document.querySelectorAll("div") (returns NodeList). Modify content: element.textContent = "text", element.innerHTML = "<b>bold</b>" (careful — XSS risk). Attributes: element.setAttribute("href", "url"), element.getAttribute(). CSS: element.style.color = "red", element.classList.add("active"), element.classList.toggle(). Create/insert elements: document.createElement(), parentEl.appendChild(child), element.insertAdjacentHTML(). Remove: element.remove().