Example 1 — Read id
Read the identifier from an element that already has one.
const hero = document.getElementById("hero");
console.log(hero.id);
// "hero"
console.log(hero.getAttribute("id"));
// "hero" (same value via the attribute) How It Works
- Find the node.
getElementById("hero")returns the element. - Read
.id. The property returns the string"hero". - Attribute match.
getAttribute("id")shows the same reflected value. - Missing id? You would get
"", notnull.

