JavaScript Document links Property

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

What You’ll Learn

Document.links is a read-only instance property that returns a live HTMLCollection of every <a> and <area> element with an href attribute. Learn MDN’s loop example, how it differs from document.anchors, live collection behavior, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

HTMLCollection

03

Matches

a / area + href

04

Live

Updates

05

Access

[0] / item()

06

Status

Baseline widely

Introduction

Want every hyperlink on a page without writing a selector? document.links gives you a ready-made collection of all <a href="..."> and <area href="..."> elements.

MDN: the read-only property returns a collection of all <a> and <area> elements in a document with a value for the href attribute.

💡
Live collection

Like document.images and document.forms, the list updates when links are added or removed from the DOM.

Related Document tutorials: anchors, linkColor, forms, Document constructor.

Understanding Document.links

A read-only instance property on Document. Its value is a live HTMLCollection of hyperlink elements.

  • Value — live HTMLCollection of a[href] and area[href] (MDN).
  • Each item — typically HTMLAnchorElement or HTMLAreaElement with an href property.
  • Accessdocument.links[0] or document.links.item(0).
  • Lengthdocument.links.length counts hyperlinks on the page.
  • Iteratefor (const link of document.links) works in modern browsers (MDN example).

📝 Syntax

JavaScript
document.links

Value

An HTMLCollection — a live list of every <a> and <area> element that has an href attribute (MDN).

MDN example

JavaScript
for (const link of document.links) {
  const linkHref = document.createTextNode(link.href);
  const lineBreak = document.createElement("br");
  document.body.appendChild(linkHref);
  document.body.appendChild(lineBreak);
}

This loop reads each link’s resolved href URL and appends it to the page. In tutorials we usually log to the console instead.

⚡ Quick Reference

GoalCode / note
Count linksdocument.links.length
First linkdocument.links[0]
Same as item()document.links.item(0)
Loop allfor (const link of document.links) { ... }
Read hrefdocument.links[i].href
MDN statusBaseline Widely available (since Jun 2018)

🔍 At a Glance

Four facts about document.links.

Type
HTMLCollection

Read-only

Items
a / area

With href

Live?
yes

Auto-updates

Status
baseline

Widely available

📋 document.links vs querySelectorAll

document.linksquerySelectorAll("a[href]")
Includes <area>YesOnly if selector includes area
Collection typeLive HTMLCollectionStatic NodeList
FilteringAll hyperlinks onlyAny CSS selector
Best forQuick full link listScoped or filtered queries

Examples Gallery

Examples follow MDN Document: links. Open try-it labs to count, loop, and inspect hyperlinks on sample pages.

📚 Getting Started

Count and inspect hyperlinks on a page.

Example 1 — Count Hyperlinks

Use length to see how many links the document contains.

JavaScript
console.log(document.links.length);
console.log("Has links?", document.links.length > 0);
Try It Yourself

How It Works

Only elements with an href attribute are counted — plain <a> tags without href are excluded.

Example 2 — MDN Loop Over Every Link

Log each link’s resolved href URL (MDN-style iteration).

JavaScript
for (const link of document.links) {
  console.log(link.href);
}
Try It Yourself

How It Works

link.href returns the fully resolved URL, even when the HTML used a relative path.

📈 Access, Maps & Live Updates

Indexed access, image maps, and dynamic DOM changes.

Example 3 — Access the First Link

Read the first hyperlink with index 0 or item(0).

JavaScript
const first = document.links[0];
console.log(first?.href);
console.log(first === document.links.item(0)); // true
Try It Yourself

How It Works

Bracket notation and item() are equivalent for HTMLCollection access.

Example 4 — Includes <area> in Image Maps

MDN includes area elements with href, not just anchors.

JavaScript
// HTML: 
for (const link of document.links) {
  console.log(link.tagName, link.href);
}
// Logs AREA and A entries
Try It Yourself

How It Works

Clickable regions in HTML image maps are hyperlinks too, so they appear in document.links.

Example 5 — Live Collection Updates

Adding a new link increases document.links.length automatically.

JavaScript
const before = document.links.length;
const a = document.createElement("a");
a.href = "#dynamic";
a.textContent = "New link";
document.body.appendChild(a);
console.log(before, "→", document.links.length);
Try It Yourself

How It Works

HTMLCollection objects are live — they reflect DOM changes without re-querying.

🚀 Common Use Cases

  • Link audits — count external vs internal URLs in a page script.
  • SEO checks — loop links and inspect href, rel, or target.
  • Accessibility reviews — verify link text and focus styles across all anchors.
  • Quick diagnostics — log every hyperlink during debugging.
  • Learning DOM collections — compare with document.images and document.forms.
  • Image map tooling — include clickable <area> regions in link inventories.

🧠 How document.links Works

1

Browser parses HTML

<a href> and <area href> elements enter the DOM.

Parse
2

Collection is built

document.links exposes a live HTMLCollection of matching elements.

Collect
3

You read or loop

Use length, indexing, or for...of to inspect each hyperlink.

Access
4

DOM changes update the list

Add or remove links and the live collection reflects the change immediately.

📝 Notes

  • MDN: Baseline Widely available (since June 2018) — no Deprecated / Experimental / Non-standard banner.
  • Requires href<a> without href is not a hyperlink and is excluded.
  • Includes <area href> in image maps, not just <a> (MDN).
  • link.href is typically an absolute URL in the browser.
  • Related: anchors, images, linkColor, Document constructor.

Universal Browser Support

Document.links 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.links

Read-only live HTMLCollection of every a and area element with href.

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.links Excellent

Bottom line: Use document.links for quick access to all hyperlinks. Prefer querySelectorAll when you need filtered or scoped link queries.

Conclusion

Document.links is a simple, live shortcut to every hyperlink on the page — both <a href> and <area href>. Use it to count, loop, and inspect link URLs, or reach for querySelectorAll when you need more specific queries.

Continue with location, ownerDocument, anchors, linkColor, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use document.links.length for a quick hyperlink count
  • Loop with for...of in modern code
  • Parse link.href with the URL API for host/path checks
  • Remember image-map <area> links are included
  • Use querySelectorAll when you need filtered subsets

❌ Don’t

  • Expect <a> without href to appear
  • Confuse document.links with deprecated document.anchors
  • Cache length across long loops if DOM may change mid-loop
  • Assume href in HTML equals the resolved link.href string
  • Skip accessibility checks on dynamically added links

Key Takeaways

Knowledge Unlocked

Five things to remember about document.links

All hyperlinks in one live collection.

5
Core concepts
📄02

Items

a / area + href

MDN
🔄03

Live

Auto-updates

MDN
🔢04

Access

[0] / item()

Equivalent
🔍05

Loop

for...of

MDN

❓ Frequently Asked Questions

A read-only HTMLCollection of every a and area element in the document that has an href attribute — in other words, every hyperlink on the page.
No. MDN marks Document.links as Baseline Widely available (since June 2018). It is a standard, supported Document property.
Anchor elements (<a href="...">) and area elements inside image maps (<area href="...">). Elements without an href value are excluded.
document.links lists hyperlinks with href. document.anchors lists named anchor targets with a name attribute (<a name="...">) and is deprecated. One element can appear in both if it has name and href.
Yes. Use for...of, a classic for loop with length and index, or spread into an array. The collection is live — it updates when matching links are added or removed.
Use document.links for every hyperlink quickly. Use querySelectorAll when you need filtered links (for example a.external), scoped queries inside a container, or a static NodeList snapshot.
Did you know?

document.links and document.anchors sound similar but serve different jobs: links lists hyperlinks with href (still standard), while anchors lists named in-page targets with name (deprecated). A single <a name="x" href="#y"> can appear in both collections.

Next: location

Learn the Document Location object that holds the page URL.

location →

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