JavaScript String anchor() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.anchor(name) is a legacy HTML wrapper that returns a new string wrapping your text in <a name="…">…</a>. The name attribute on anchors is obsolete — use id instead.

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

Syntax

Call string.anchor. Start with this line.

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

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

Library declarations
JavaScript
string.anchor(name)

Parameters

ParameterDescription
nameRequired. Value of the name attribute

Return Value

Return ValueDescription
*HTML string with <a name> tags

JavaScript Examples

Example 1 — Basic Wrap

Call anchor() to build the classic <a name> HTML string.

JavaScript
const text = "Hello";

console.log(text.anchor("hi"));
// '<a name="hi">Hello</a>'
Try It Yourself

How It Works

  1. Start with text. "Hello" becomes the content between the tags.
  2. Pass a name. "hi" fills the name attribute.
  3. Get HTML markup. The return value is '<a name="hi">Hello</a>'.
  4. Original stays put. text is still "Hello".

Example 2 — Quote Escape in name

Double quotes inside the name are escaped as &quot;.

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

console.log(typeof "x".anchor("n")); // "string"
Try It Yourself

How It Works

  1. Name has a quote. a"b would break a raw attribute.
  2. Engine escapes it. " becomes &quot; in the result.
  3. Type is string. You get text, not a live DOM element.
  4. Still obsolete markup. Escaping does not make name on <a> valid HTML.

Example 3 — Prefer DOM + id

Compare the legacy wrapper with a modern in-page target.

JavaScript
// Legacy — do not use in new code
console.log("Intro".anchor("intro"));
// '<a name="intro">Intro</a>'

// Prefer id on a real element
const heading = document.createElement("h2");
heading.id = "intro";
heading.textContent = "Intro";
console.log(heading.outerHTML);
// '<h2 id="intro">Intro</h2>'
Try It Yourself

How It Works

  1. Legacy path. anchor("intro") builds obsolete name markup.
  2. Create a real element. Any element can hold an id.
  3. Set plain text. textContent is safer than injecting markup strings.
  4. Link with hash. Use href="#intro" to jump to that id.

Browser & Runtime Support

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

✓ Deprecated · Legacy

String.prototype.anchor()

Available in major browsers and Node.js for legacy compatibility. Prefer element id targets and DOM APIs for 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
anchor() Avoid in new code

Bottom line: Recognize anchor() when reading old code. For new UI, set an element id and link with #id — never build obsolete name anchors with string wrappers.

Frequently Asked Questions

String.prototype.anchor(name) returns an HTML string that wraps the text in an <a> tag with a name attribute — for example "Hello".anchor("hi") returns '<a name="hi">Hello</a>'.
Yes. MDN marks all HTML wrapper methods as deprecated. Prefer document.createElement() and modern id-based links instead of building HTML with string methods.
The HTML specification no longer allows the name attribute on <a> elements. Named anchors are obsolete; use id on any element and link with #id instead.
No. Strings are immutable. anchor() returns a new string containing HTML markup. The original text stays the same.
Double quotes in name are escaped as &quot; so the generated attribute stays well-formed as HTML text.
It remains widely available for compatibility, but you should not use it in new code. Prefer DOM APIs and id-based anchors.

Next: big()

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

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

8 people found this page helpful