Example 1 — Read head and Compare (MDN)
Log the head node and confirm it matches querySelector("head").
const theHead = document.head;
console.log(theHead.tagName); // "HEAD"
console.log(theHead === document.querySelector("head")); // true How It Works
- Grab the node.
document.headis the<head>element. - Check
tagName. For HTML it is the uppercase string"HEAD". - Compare with a selector. On a normal page both expressions point to the same object.
- Prefer
document.head. Shorter and clearer thanquerySelector("head").

