anchor()
Instance method
Wraps a string in <a name> HTML markup you can inspect and learn from.

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().
Instance method
Wraps a string in <a name> HTML markup you can inspect and learn from.
HTML string
Produces markup text only — not a live DOM node you can append.
Legacy only
Kept for backward compatibility. Recognize it, but avoid it in new code.
Single param
Coerced to a string and placed in the name attribute; quotes are escaped.
No mutation
Returns a brand-new string — the original text is never changed.
Modern swap
Build real <a> elements with id and href instead.
Early JavaScript shipped several HTML wrapper methods that turned a string into a markup snippet — bold(), italics(), anchor(), and more. They made it quick to build HTML for innerHTML.
Today those wrappers are deprecated. anchor() still runs in browsers for compatibility, but the markup it builds is no longer valid HTML.
The name attribute is no longer allowed on <a> elements, so the string anchor() produces is obsolete markup.
Builds an <a name> string, not a live DOM node.
The original string is never changed by the call.
The name attribute is invalid on <a> today.
Use createElement() and id-based fragments.
In short: Learn anchor() so you can read and migrate legacy code — but build links with the DOM in anything new.
anchor() MethodCalling 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>.
name become ".General form of String.prototype.anchor:
str.anchor(name) name attribute in the generated <a> start tag.A string beginning with <a name="..."> (quotes in name escaped as "), then the text of str, then </a>.
"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"; | 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 |
Four facts to remember about String.anchor().
stringHTML markup text
deprecatedCompatibility only
<a name>Obsolete attribute
createElementReal DOM nodes
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 |
Examples follow MDN String.anchor() patterns. Use View Output or Try It Yourself for each case.
See the exact HTML string anchor() builds.
anchor()MDN-style call that wraps text with a name attribute.
const contentString = "Hello, world";
contentString.anchor("hello");
// 'Hello, world' The method returns markup text only. Nothing is added to the document until you assign it somewhere (for example innerHTML).
nameDouble quotes inside name are escaped for safer HTML attributes.
"Title".anchor('section"1');
// 'Title' Escaping keeps the attribute delimiter intact when the returned string is parsed as HTML. It does not make the name attribute valid in modern HTML.
Coercion, injecting markup, and the modern replacement.
name Is Coerced to a StringNon-string names are converted the usual way.
Numbers and booleans become their string forms. An empty receiver still produces opening and closing tags.
innerHTML (Legacy)How older demos used the returned string — still produces obsolete markup.
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 Even if the browser parses the string, the attribute is obsolete. Prefer id-based fragments for in-page links.
createElement()MDN’s recommended approach — build a real element instead of HTML text.
const contentString = "Hello, world";
const elem = document.createElement("a");
elem.id = "hello";
elem.textContent = contentString;
elem.href = "#hello";
// document.body.appendChild(elem);
console.log(elem.outerHTML);
// 'Hello, world' You get a live node, valid attributes, and no string-built HTML injection. Use id (not name) as the fragment target.
Where you’ll actually meet anchor() today — mostly when reading and modernizing older code.
Recognize HTML wrapper methods when they appear in old samples and books.
Example: Spotting anchor(), bold(), or fontcolor() in decade-old scripts.
Replace deprecated wrapper calls with real DOM creation while refactoring.
Example: Swapping str.anchor(id) for document.createElement("a").
Demonstrate that string methods return new values instead of mutating.
Example: Showing the original text is unchanged after calling anchor().
Build jump links the modern way with an id and a fragment.
Example: id="section" on a heading + href="#section".
Prefer DOM APIs over building HTML strings for innerHTML.
Example: Using textContent and appendChild to avoid injection.
Never reach for wrapper methods to build navigation in fresh projects.
Example: Choosing DOM/framework components instead of string HTML.
Pro Tip: Treat anchor() as read-only knowledge — understand it, then reach for the DOM every time you build something new.
anchor() Builds MarkupYou call str.anchor(name) on a string receiver.
Double quotes become " in the attribute.
Build <a name="..."> + text + </a>.
Create a real <a> with id / href for valid pages.
anchor() is deprecated — standardized only for compatibility.name attribute on <a> is not valid in modern HTML.big(), blink(), bold(), fixed(), …) share the same fate.id + fragment links for in-page navigation.innerHTML.String.prototype.anchor() is still widely implemented for compatibility, but it is deprecated. Do not use it in new projects.
Available in modern browsers for old code, but MDN recommends document.createElement() instead.
Bottom line: Recognize anchor() in legacy samples. For new UI, create elements with the DOM and use id-based fragments — never rely on name attributes from HTML wrapper methods.
String.prototype.anchor() builds an obsolete <a name="..."> HTML string. It’s handy for understanding history and migrating old code — not for writing new features.
Continue with big(), at(), the String methods hub, the String constructor, or length.
In new code, create real elements with document.createElement() and use id + #fragment links.
document.createElement("a") for real linksid on targets and link with href="#id"textContent over string-built HTMLanchor() when you touch legacy filesanchor() in new production codename on <a> is valid HTMLanchor() output via innerHTMLanchor() to return a live elementString.anchor()Deprecated HTML wrapper — prefer createElement.
HTML string
APIdeprecated
Legacyinvalid
HTMLcreateElement
Modernno
ImmutableWrapper methods like anchor(), bold(), and fontcolor() are older than the DOM APIs you use every day! They only survive so decades-old scripts don’t break — not because they’re a good way to build pages. Basically living fossils. 🦕
Return to the hub for slice, trim, replace, and more.
8 people found this page helpful