String.prototype.anchor() is a deprecated HTML wrapper method (same API as MDN String.prototype.anchor()). It returns a string that wraps your text in an <a name="..."> tag. Learn what it builds, why that markup is obsolete, and how to replace it with document.createElement() — with five examples and try-it labs.
01
Kind
Instance method
02
Returns
HTML string
03
Status
Deprecated
04
Param
name
05
Mutates?
No
06
Prefer
createElement
Fundamentals
Introduction
Early JavaScript included several helpers that turned a string into an HTML snippet — bold(), italics(), anchor(), and others. They made it easy to inject markup with innerHTML.
Today those wrappers are deprecated. anchor() still works in browsers for compatibility, but the HTML it produces is no longer valid: the name attribute is not allowed on <a> anymore.
💡
Learn it, don’t ship it
Study anchor() so you can recognize legacy code. For new pages, build links and targets with the DOM (document.createElement("a")) and use id + #fragment instead of name.
Calling str.anchor(name) does not create a live DOM node. It concatenates an HTML string: an opening <a name="..."> 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 name become ".
Prefer DOM APIs for real elements in the page.
Foundation
📝 Syntax
General form of String.prototype.anchor:
JavaScript
str.anchor(name)
Parameters
name — a string (or value coerced to string) used as the name attribute in the generated <a> start tag.
Return value
A string beginning with <a name="..."> (quotes in name escaped as "), then the text of str, then </a>.
Common patterns
JavaScript
"Hello, world".anchor("hello");
// 'Hello, world'
"Title".anchor('section"1');
// 'Title'
// Prefer this in new code:
const elem = document.createElement("a");
elem.id = "hello";
elem.textContent = "Hello, world";
Cheat Sheet
⚡ Quick Reference
Goal
Code
Legacy HTML string
str.anchor("id")
Escape quotes in name
Automatic (" → ")
Modern element
document.createElement("a")
Modern link target
elem.id = "hello" + href="#hello"
Use in new apps?
No — deprecated
Snapshot
🔍 At a Glance
Four facts to remember about String.anchor().
Returns
string
HTML markup text
Status
deprecated
Compatibility only
Tag
<a name>
Obsolete attribute
Replace with
createElement
Real DOM nodes
Compare
📋 anchor() vs createElement()
str.anchor(name)
document.createElement("a")
Result
HTML string
Live <a> element
Valid HTML today?
No (name on <a>)
Yes (use id / href)
Recommended?
No
Yes
Best for
Reading legacy code
New UI / apps
Hands-On
Examples Gallery
Examples follow MDN String.anchor() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
See the exact HTML string anchor() builds.
Example 1 — Basic anchor()
MDN-style call that wraps text with a name attribute.
Numbers and booleans become their string forms. An empty receiver still produces opening and closing tags.
Example 4 — Injecting with innerHTML (Legacy)
How older demos used the returned string — still produces obsolete markup.
JavaScript
const contentString = "Hello, world";
const html = contentString.anchor("hello");
// document.body.innerHTML = html; // legacy pattern — avoid in new apps
console.log(html);
// Warning: is invalid in modern HTML
String.prototype.anchor() builds an obsolete <a name="..."> HTML string. It is useful for understanding history and migrating old code — not for writing new features.
Mix HTML wrappers with modern component frameworks
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.anchor()
Deprecated HTML wrapper — prefer createElement.
5
Core concepts
📝01
Returns
HTML string
API
⚠️02
Status
deprecated
Legacy
❌03
Markup
invalid
HTML
✓04
Replace
createElement
Modern
⚡05
Mutates
no
Immutable
❓ 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. They exist only for compatibility. Prefer document.createElement() (or 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.
Did you know?
HTML wrapper methods like anchor(), bold(), and fontcolor() predate widespread DOM APIs. They remain in the language mainly so decades-old scripts do not break — not because they are a good way to build pages today.