Example 1 — Match One Class
Collect every element with class test and read the first one.
// HTML: <p class="test">A</p><p class="test">B</p>
const list = document.getElementsByClassName("test");
console.log(list.length); // 2
console.log(list[0].textContent); // "A"
console.log(list.constructor.name); // "HTMLCollection" How It Works
- Pass one class. No leading dot — just the name string.
- Get a collection. Not a single Element (unless you index it).
- Read length. Count matches without converting to an array.
- Index [0]. First match, or
undefinedif empty.

