JavaScript Document head Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.head is a read-only instance property that returns the <head> element as an HTMLHeadElement. Learn MDN’s id / querySelector example, how to append meta and link tags, how it differs from document.body, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

HTMLHeadElement

03

HTML

<head>

04

vs

document.body

05

Common

meta / link

06

Status

Baseline widely

Introduction

Every HTML page has a <head> section for metadata: <title>, <meta>, stylesheets, and scripts. In JavaScript, that node is document.head.

MDN: the read-only property returns the <head> element of the current document as an HTMLHeadElement. It is the standard shortcut instead of always calling document.querySelector("head").

💡
Head vs body

Use document.head for page metadata and resources loaded from the head. Use document.body for visible content the user sees on the page.

Related Document tutorials: body, documentElement, Document constructor.

Understanding Document.head

A read-only instance property on Document. Its value is the document’s HTMLHeadElement.

  • Value — the <head> element (MDN).
  • Read-only — assigning fails silently or throws TypeError in strict mode (MDN).
  • Equivalent — for HTML, usually the same node as document.querySelector("head") (MDN).
  • Children — title, meta, link, style, script, and other head-only elements.
  • Mutate contents — you cannot replace document.head, but you can append to it with DOM methods.

📝 Syntax

JavaScript
document.head

Value

An HTMLHeadElement representing the document’s <head> (MDN).

MDN example

JavaScript
const theHead = document.head;

console.log(theHead.id); // "my-document-head"
console.log(theHead === document.querySelector("head")); // true

⚡ Quick Reference

GoalCode / note
Get head elementdocument.head
Read id / tagNamedocument.head.id
Append meta tagdocument.head.appendChild(meta)
Append stylesheetdocument.head.appendChild(link)
Same as querySelector?document.head === document.querySelector("head")
MDN statusBaseline Widely available (since Jun 2018)

🔍 At a Glance

Four facts about document.head.

Type
HTMLHeadElement

Read-only

HTML
<head>

Metadata

Assign?
no

Strict throws

Status
baseline

Widely available

📋 head vs body

document.headdocument.body
Element<head><body>
Visible?No (metadata)Yes (content)
Read-only ref?Yes (MDN)No — settable
Typical childrenmeta, link, title, scriptdiv, p, main, etc.

Examples Gallery

Examples follow MDN Document: head. You can mutate head contents even though the property itself is read-only.

📚 Getting Started

Read the head element and compare with querySelector.

Example 1 — MDN: Read document.head

Log the id and confirm it matches querySelector("head").

JavaScript
const theHead = document.head;

console.log(theHead.id);
console.log(theHead === document.querySelector("head"));
Try It Yourself

How It Works

MDN shows both accessors return the same HTMLHeadElement in normal HTML documents.

Example 2 — Append a <meta> Tag

Add viewport or description metadata at runtime.

JavaScript
const meta = document.createElement("meta");
meta.name = "description";
meta.content = "Added from JavaScript via document.head";

document.head.appendChild(meta);
console.log(document.head.querySelector('meta[name="description"]').content);
Try It Yourself

How It Works

The property is read-only, but the head node is a normal parent you can append to.

📈 Styles, Title & Inventory

Inject resources and inspect what lives in the head.

Example 4 — Read the Page Title from Head

Access the <title> inside document.head.

JavaScript
const titleEl = document.head.querySelector("title");
console.log(titleEl ? titleEl.textContent : "(no title)");

// You can also use document.title for the same string
console.log(document.title);
Try It Yourself

How It Works

document.title is a convenience shortcut, but the title node lives under document.head.

Example 5 — List Head Children

Inventory meta, link, script, and style tags.

JavaScript
const tags = [...document.head.children].map((el) => el.tagName);
console.log(tags.join(", "));
console.log("child count:", document.head.childElementCount);
Try It Yourself

How It Works

Useful when debugging which scripts or styles loaded into the head.

🚀 Common Use Cases

  • SEO meta tags — inject description, Open Graph, or robots meta at runtime.
  • Dynamic stylesheets — load themes or fonts with <link>.
  • Script loading — append deferred or module scripts to the head.
  • Title updates — read or change the <title> for SPA route changes.
  • Debugging — list head children to see what the page loaded.
  • Feature flags — add meta tags consumed by CSS or other scripts.

🧠 How document.head Fits the DOM

1

Browser parses HTML

It builds <html>, then <head> and <body>.

Parse
2

You read document.head

Returns the live HTMLHeadElement for that document (MDN).

Access
3

Append or query children

Use appendChild, querySelector, or children on the head node.

Mutate
4

Property stays read-only

You cannot assign a new head element to document.head (MDN). Change contents, not the reference.

📝 Notes

  • MDN: Baseline Widely available (since June 2018) — no Deprecated / Experimental / Non-standard banner.
  • Read-only property; assignment fails silently or throws in strict mode (MDN).
  • Usually identical to document.querySelector("head") in HTML documents (MDN).
  • Pair with document.body for the full page structure.
  • Related: body, documentElement, ownerDocument, Document constructor.

Universal Browser Support

Document.head is marked Baseline Widely available on MDN (since June 2018). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.head

Read-only reference to the HTML head element — standard for metadata, styles, and scripts.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE
Full support
Document.head Excellent

Bottom line: Use document.head to access and update head contents. Prefer appendChild for meta, link, and script tags instead of replacing the entire head.

Conclusion

Document.head is the standard handle for the <head> element. Use it to read metadata, append meta/link/script tags, and inspect head resources—while remembering the property itself is read-only.

Continue with hidden, body, documentElement, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use document.head instead of repeated querySelector("head")
  • Append meta, link, and script tags when loading resources dynamically
  • Check for existing tags before duplicating meta or link entries
  • Pair with document.body for full page structure
  • Use document.title for quick title reads/writes

❌ Don’t

  • Assign to document.head — it is read-only (MDN)
  • Put visible page content in the head
  • Append duplicate viewport or charset meta tags blindly
  • Confuse head with document.documentElement (the <html> root)
  • Replace the entire head unless you fully control the document

Key Takeaways

Knowledge Unlocked

Five things to remember about document.head

Your shortcut to the document’s metadata section.

5
Core concepts
📝02

HTML

<head>

Metadata
🔒03

Read-only

ref only

MDN
04

Append

meta / link

Contents OK
⚖️05

vs

body

Head vs content

❓ Frequently Asked Questions

The HTMLHeadElement of the current document — the DOM node for the <head> element (MDN).
No. MDN marks Document.head as Baseline Widely available (since June 2018). It is a standard, widely used DOM property.
No. MDN notes the property is read-only. Assigning fails silently in non-strict mode, or throws a TypeError in strict mode.
For normal HTML documents, yes — MDN's example shows document.head === document.querySelector('head') is true.
document.head is the <head> section (metadata, title, styles, scripts). document.body is the visible page content container.
When you need to add or read meta tags, link stylesheets, inject scripts, or update the document title from JavaScript.
Did you know?

Browsers create a <head> element even if your HTML omits it. That means document.head is almost always available on HTML documents, though you should still write explicit head markup for charset, viewport, and title.

Next: hidden

Learn how to detect background tabs with document.hidden.

hidden →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful