JavaScript Document links Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

Document.links is a read-only HTMLCollection of every <a> and <area> element in the document that has an href attribute (MDN).

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.links. Start with this line.

JavaScript
document.links
CallWhat it does
document.linksPrimary signature.

Built into the browser. Read or assign document.links — no import required.

Library declarations
JavaScript
document.links

Parameters

ParameterDescription
linksRequired. Live HTMLCollection of a/area with href
Snapshot copyOptional. Array.from(document.links) if mutating while looping

Return Value

Return ValueDescription
*The current value of document.links.

JavaScript Examples

Example 1 — Count Hyperlinks

Read how many a/area elements with href exist.

JavaScript
console.log(document.links.length);
console.log(document.links[0] && document.links[0].tagName);
Try It Yourself

How It Works

  1. Read the collection. document.links is an HTMLCollection.
  2. Use length. Counts every hyperlink with an href.
  3. Index the first item. links[0] is the first match (or undefined if empty).
  4. Check the tag. Usually "A"; image maps may contribute "AREA".

Example 2 — Loop Over Link URLs

Walk each hyperlink and read its absolute href (MDN-style loop).

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

How It Works

  1. Iterate with for…of. Works on HTMLCollection in modern browsers.
  2. Read link.href. Returns the resolved absolute URL string.
  3. Order. Document order — top to bottom in the markup.
  4. Live list. If you add/remove matching links during a long loop, prefer a snapshot copy.

Example 3 — links vs querySelectorAll

Compare the live collection with a static NodeList of anchors.

JavaScript
const live = document.links;
const snap = document.querySelectorAll("a[href]");

console.log(live.length);
console.log(snap.length);
console.log(live.constructor.name);
console.log(snap.constructor.name);
Try It Yourself

How It Works

  1. Live collection. document.links also includes <area href>.
  2. Static snapshot. querySelectorAll("a[href]") is only <a> and does not auto-update.
  3. Counts may match when there are no <area> elements.
  4. Choose by need. All hyperlinks → links; filters/scopes → querySelectorAll.

Browser Support

Document.links is Baseline Widely available on MDN (since June 2018). It works across modern browsers as a standard Document collection.

✓ Baseline · Widely available

Document.links

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

99% Browser support
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Android WebView Supported · Modern WebView
Full support
links Excellent

Bottom line: Safe for teaching and production. Prefer document.links for all hyperlinks; use querySelectorAll when you need CSS filters or a static snapshot.

Frequently Asked Questions

A read-only HTMLCollection of every <a> and <area> element in the document that has an href attribute — every hyperlink on the page (MDN).
No. MDN marks Document.links as Baseline Widely available (since June 2018). It is a standard Document collection property.
Anchor elements (<a href="...">) and image-map areas (<area href="...">). Elements without an href value are excluded.
document.links lists hyperlinks with href. document.anchors lists named anchors with a name attribute and is deprecated. Prefer links for hyperlink work.
Yes. Like other HTMLCollections, it updates when matching links are added or removed. Copy with Array.from if you mutate the DOM while looping.
Use document.links for every hyperlink quickly. Use querySelectorAll when you need filters (for example a.external), a scoped search, or a static NodeList snapshot.

Next: location

Learn how to read and work with the document’s Location object.

location tutorial →

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.

12 people found this page helpful