JavaScript String link() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated

What You’ll Learn

String.prototype.link() is a deprecated HTML wrapper method (same API as MDN String.prototype.link()). It returns a string that wraps your text in an <a href="..."> tag. Learn quote escaping, how it differs from anchor(), and how to replace it with the DOM — with five examples and try-it labs.

01

Kind

Instance method

02

Returns

HTML string

03

Status

Deprecated

04

Param

url (href)

05

Mutates?

No

06

Prefer

createElement

Introduction

Early JavaScript included helpers that wrapped text in HTML tags — link(), anchor(), bold(), italics(), and others. link() built a hyperlink string you could drop into innerHTML.

Today those wrappers are deprecated. link() may still return a string for compatibility. Real <a href> links remain essential HTML, but you should create them with the DOM — not string methods.

💡
Learn it, don’t ship it

Study link() so you can recognize legacy code. For new pages, use document.createElement("a"), set href and textContent, then append the element.

This page is part of JavaScript String Methods. Related topics include anchor() and italics().

Understanding the link() Method

Calling str.link(url) does not create a live DOM node. It concatenates an HTML string: an opening <a href="..."> tag, the original text, and a closing </a>.

  • It is an instance method on strings (auto-boxed if needed).
  • It returns a new string — the original is unchanged.
  • Double quotes inside url become &quot;.
  • Ampersands (&) are not auto-escaped — prefer &amp; when building attribute text by hand.
  • Prefer DOM APIs for real links in the page.

📝 Syntax

General form of String.prototype.link:

JavaScript
str.link(url)

Parameters

  • url — any string used as the href attribute (relative or absolute URL). Coerced to a string if needed. MDN notes that & characters should be written as &amp; in HTML attribute text.

Return value

A string beginning with <a href="..."> (quotes in url escaped as &quot;), then the text of str, then </a>.

SituationReturns
Normal URL"Hi".link("https://example.com")'<a href="https://example.com">Hi</a>'
Quotes in URL"x".link('a"b')'<a href="a&quot;b">x</a>'
Empty link text"".link("https://example.com") → empty content between tags
Type of resultAlways a string (not a DOM node)

Common patterns

JavaScript
"MDN Web Docs".link("https://developer.mozilla.org/");
// 'MDN Web Docs'

"x".link('a"b');
// 'x'

// Prefer this in new code:
const elem = document.createElement("a");
elem.href = "https://developer.mozilla.org/";
elem.textContent = "MDN Web Docs";
// document.body.appendChild(elem);

⚡ Quick Reference

GoalCode
Legacy HTML stringstr.link(url)
Result shape<a href="...">...</a>
Escape quotes in URLAutomatic ("&quot;)
Modern DOMdocument.createElement("a")
Set destinationelem.href = url
Use in new apps?No — deprecated

🔍 At a Glance

Four facts to remember about String.link().

Returns
string

HTML markup text

Status
deprecated

Compatibility only

Tag
<a href>

Still valid HTML

Replace with
createElement

DOM, not strings

📋 link() vs anchor() vs DOM

str.link(url)str.anchor(name)createElement("a")
Attributehref (destination)name (legacy named target)You set href / id yourself
ResultHTML stringHTML stringLive DOM element
Valid today?Tag OK; method deprecatedname on <a> is obsoleteYes
Recommended?NoNoYes

Examples Gallery

Examples follow MDN String.link() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

See the exact HTML string link() builds.

Example 1 — Basic link()

MDN-style call that wraps text in <a href>.

JavaScript
const contentString = "MDN Web Docs";
contentString.link("https://developer.mozilla.org/");
// 'MDN Web Docs'
Try It Yourself

How It Works

The method returns markup text only. Nothing is added to the document until you assign it somewhere (for example innerHTML).

Example 2 — Quote Escaping and Empty Text

Double quotes in the URL become &quot;; empty text still produces tags.

JavaScript
"x".link('a"b');
// 'x'

"".link("https://example.com");
// ''

typeof "Hi".link("https://example.com");  // "string"
Try It Yourself

How It Works

Quote escaping keeps the attribute syntactically closed. Link text is not HTML-escaped the same way — another reason to avoid injecting untrusted strings via innerHTML.

📈 Practical Patterns

Legacy injection patterns and the modern DOM replacement.

Example 3 — Injecting with innerHTML (Legacy)

How older demos used the returned string — prefer the DOM in new apps.

JavaScript
const contentString = "MDN Web Docs";
const html = contentString.link("https://developer.mozilla.org/");
// document.body.innerHTML = html;  // legacy pattern — avoid in new apps

console.log(html);
// Prefer createElement("a") in new apps
Try It Yourself

How It Works

The returned string is only markup text. Prefer creating a real <a> element with the DOM instead of assigning HTML strings.

Example 4 — Other HTML Wrappers Nearby

Recognize the same pattern family — all deprecated.

JavaScript
"Hi".link("https://example.com"); // 'Hi'
"Hi".anchor("section1");          // 'Hi'
"Hi".bold();                      // 'Hi'
"Hi".italics();                   // 'Hi'
Try It Yourself

How It Works

link() and anchor() both build <a> markup, but for different attributes. Modern code creates one element and sets properties explicitly.

Example 5 — Modern Replacement with the DOM

MDN’s recommended approach — create a real link element instead of an HTML string.

JavaScript
const contentString = "MDN Web Docs";
const elem = document.createElement("a");
elem.href = "https://developer.mozilla.org/";
elem.textContent = contentString;

// document.body.appendChild(elem);
console.log(elem.tagName);
console.log(elem.href);
console.log(elem.outerHTML);
Try It Yourself

How It Works

You get a live element you can style, set rel/target on, and keep separate from untrusted HTML concatenation. Prefer textContent over building markup by hand.

🚀 Common Use Cases

  • Reading legacy tutorials — recognize HTML wrapper methods in old samples.
  • Migrating old scripts — replace link() with createElement("a").
  • Teaching string immutability — show that methods return new strings.
  • Not for new apps — do not use link() to build hyperlinks in new apps.
  • Safer markup — prefer textContent + DOM properties over innerHTML strings.
  • Contrast with anchor()link = href; anchor = obsolete name.

🧠 How link() Builds Markup

1

Start with text + URL

You call str.link(url) on a string receiver.

Input
2

Escape quotes in URL

Replace " with &quot; inside the href value.

Escape
3

Return an HTML string

Build <a href="..."> + text + </a> — no DOM node yet.

Result
4

Prefer the DOM

Create a real <a> with href and textContent.

📝 Notes

  • link() is deprecated — standardized only for compatibility.
  • The method is deprecated; the <a href> element itself remains valid HTML.
  • It returns a string, not a DOM node.
  • Double quotes in url are escaped as &quot;.
  • Ampersands are not auto-escaped — follow MDN and use &amp; when writing attribute HTML by hand.
  • Other HTML wrappers (anchor(), bold(), italics(), …) share the same fate.
  • Avoid assigning untrusted strings to innerHTML.

Browser & Runtime Support

String.prototype.link() is still widely implemented for compatibility, but it is deprecated. Do not use it in new projects.

Deprecated · Legacy

String.link()

Available in modern browsers for old code, but MDN recommends DOM APIs such as createElement() instead.

Legacy Compatibility only
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
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
link() Avoid in new code

Bottom line: Recognize link() in legacy samples. For new UI, create hyperlinks with the DOM — never rely on HTML wrapper string methods.

Conclusion

String.prototype.link() builds an <a href="...">...</a> HTML string. The method is deprecated — useful for history and migrating old code — not for writing new features.

Continue with small(), at(), or the String methods hub.

💡 Best Practices

✅ Do

  • Use document.createElement("a") for real links
  • Set href and textContent separately
  • Prefer textContent over string-built HTML
  • Replace link() when you touch legacy files
  • Learn wrappers only to recognize deprecated APIs

❌ Don’t

  • Use link() in new production code
  • Assume string-built HTML is the best way to make links
  • Inject untrusted link() output via innerHTML
  • Expect link() to return a live element
  • Mix HTML wrappers with modern component frameworks

Key Takeaways

Knowledge Unlocked

Five things to remember about String.link()

Deprecated HTML wrapper — prefer createElement("a").

5
Core concepts
⚠️ 02

Status

deprecated

Legacy
🔗 03

Markup

<a href> OK

HTML
04

Replace

createElement

Modern
05

Mutates

no

Immutable

❓ Frequently Asked Questions

String.prototype.link(url) returns an HTML string that wraps the text in an <a> tag with an href attribute — for example "Hello".link("https://example.com") returns '<a href="https://example.com">Hello</a>'.
Yes. MDN marks all HTML wrapper methods as deprecated. Prefer document.createElement("a"), set href and textContent, then append the element.
Yes. Hyperlinks are core HTML. The method is deprecated — not the <a> tag. Build links with the DOM instead of string wrappers.
Yes. Double quotes in url become &quot; so the generated attribute stays well-formed as HTML text. Ampersands (&) are not auto-escaped — MDN recommends using &amp; when needed.
No. Strings are immutable. link() returns a new string containing HTML markup. The original text stays the same.
Use document.createElement("a"), set elem.href and elem.textContent (or textContent + setAttribute), then appendChild. Avoid building links with innerHTML from untrusted strings.
Did you know?

link() and anchor() both produce <a> tags, but for opposite jobs: link sets href (where you go), while legacy anchor set name (where you land). Today, destinations use id on any element — not name on <a>.

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

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.

8 people found this page helpful