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)
Call
What 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
Parameter
Description
name
Required. Value of the name attribute
Return Value
Return Value
Description
*
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>'
Type is string. You get text, not a live DOM element.
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>'
Legacy path.anchor("intro") builds obsolete name markup.
Create a real element. Any element can hold an id.
Set plain text.textContent is safer than injecting markup strings.
Link with hash. Use href="#intro" to jump to that id.
Compatibility
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.
LegacyCompatibility only
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · 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 " 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.