Example 1 — Loop with for...of (MDN)
Log every child element’s tag name.
const myElement = document.getElementById("foo");
for (const child of myElement.children) {
console.log(child.tagName);
}
// P
// SPAN How It Works
- Find the parent.
getElementByIdreturns the container. - Read children. You get a live
HTMLCollectionof element kids only. - Loop with for...of. Each iteration is an
Element. - Log tagName. Tag names are uppercase in HTML documents (for example
"P").

