Example 1 — Enumerate with for...of (MDN)
Print every attribute name and value on a paragraph.
const paragraph = document.getElementById("paragraph");
if (paragraph.hasAttributes()) {
for (const attr of paragraph.attributes) {
console.log(`${attr.name} -> ${attr.value}`);
}
}
// id -> paragraph
// class -> green
// contenteditable -> How It Works
- Find the element.
getElementByIdreturns the paragraph. - Guard with hasAttributes. Skip the loop if there is nothing to show (MDN).
- Iterate the map.
for...ofyields eachAttrnode. - Read name and value. Boolean attributes like
contenteditableoften have an empty value.

