document.getElementById("elementId");
<h1 id="title">Welcome to Learn2Kode</h1>
const heading = document.getElementById("title");
console.log(heading.textContent);
document.querySelector("selector");
<p class="intro">JavaScript is powerful</p>
<p class="intro">JavaScript is powerful</p>
document.querySelector("#title");
document.querySelector(".box");
document.querySelector("p");
// Access an element by its ID
const heading = document.getElementById("main-heading");
console.log(heading.textContent);
document.querySelector("title"); // Wrong
document.querySelector("#title"); // Correct
document.getElementById("box"); // Works only if box is an ID
The DOM is the backbone of interactive web pages. Understanding it is essential for any JavaScript developer, whether you are working with vanilla JS or modern frameworks.
What’s Next?