JavaScript String link() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.link(url) is a legacy HTML wrapper that returns a new string wrapping your text in <a href="…">…</a>. It never mutates the original string.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.link. Start with this line.

JavaScript
string.link(url)
CallWhat it does
string.link(url)Primary signature.
string.link(...)Call with the arguments your case needs.

Built into JavaScript. Call link(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
string.link(url)

Parameters

ParameterDescription
urlRequired. Value for the href attribute

Return Value

Return ValueDescription
*HTML string with <a href> tags

JavaScript Examples

Example 1 — Basic Wrap

Call link(url) to build the classic <a href> HTML string.

JavaScript
const text = "MDN Web Docs";
const url = "https://developer.mozilla.org/";

console.log(text.link(url));
// '<a href="https://developer.mozilla.org/">MDN Web Docs</a>'
Try It Yourself

How It Works

  1. Start with label text. "MDN Web Docs" becomes the link content.
  2. Pass the URL. link(url) puts it into the href attribute.
  3. Get HTML markup. You receive a string, not a live element.
  4. Original stays put. text is still "MDN Web Docs".

Example 2 — Quote Escaping in url

Double quotes in the URL become &quot; so the attribute stays well-formed.

JavaScript
console.log("x".link('a"b'));
// '<a href="a&quot;b">x</a>'

console.log("".link("https://example.com"));
// '<a href="https://example.com"></a>'

console.log(typeof "Hi".link("https://x.test")); // "string"
Try It Yourself

How It Works

  1. Escape quotes. A " inside url becomes &quot; in the markup text.
  2. Empty label. Tags are still produced; content between them is empty.
  3. Type is string. You get text, not a DOM node you can click yet.
  4. Ampersands. & is not auto-escaped — write &amp; when the URL needs it.

Example 3 — Prefer the DOM Instead

Compare the legacy wrapper with createElement("a").

JavaScript
// Legacy — do not use in new code
console.log("Docs".link("https://example.com"));
// '<a href="https://example.com">Docs</a>'

// Prefer a real element
const el = document.createElement("a");
el.href = "https://example.com";
el.textContent = "Docs";
console.log(el.tagName);          // "A"
console.log(el.getAttribute("href")); // "https://example.com"
console.log(el.textContent);      // "Docs"
Try It Yourself

How It Works

  1. Legacy path. link() builds an HTML string via the deprecated method.
  2. Create a real element. createElement("a") makes a live node.
  3. Set href and text safely. Assign properties instead of concatenating markup.
  4. Rule of thumb. Learn link() to read old code; ship DOM APIs for new UI.

Browser & Runtime Support

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

✓ Deprecated · Legacy

String.prototype.link()

Available in major browsers and Node.js for legacy compatibility. Prefer document.createElement("a") in new code.

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() when reading old code. For new UI, use createElement("a") with href and textContent — never ship wrapper strings from string methods.

Frequently Asked Questions

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 — use &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, then appendChild. Avoid building links with innerHTML from untrusted strings.

Next: small()

Another deprecated HTML wrapper — this one builds <small> markup from a string.

small() 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.

7 people found this page helpful