JavaScript String sup() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated

What You’ll Learn

String.prototype.sup() is a deprecated HTML wrapper method (same API as MDN String.prototype.sup()). It returns a string that wraps your text in a <sup> tag. Learn what it builds, why wrappers are discouraged, and how to replace them with DOM APIs — with five examples and try-it labs.

01

Kind

Instance method

02

Returns

HTML string

03

Status

Deprecated

04

Params

None

05

Mutates?

No

06

Prefer

createElement

Introduction

Early JavaScript included helpers that wrapped text in HTML tags — sup(), sup(), bold(), italics(), and others. They made it easy to inject markup with innerHTML.

Today those wrappers are deprecated. sup() still works in browsers for compatibility. The <sup> element remains valid HTML (superscript for exponents, footnotes, and math-like text), but you should create it with the DOM — not by concatenating tags as a string.

💡
Learn it, don’t ship it

Study sup() so you can recognize legacy code. For new pages, use document.createElement("sup"). Pair it with sub() only when reading old formula markup — both wrappers are deprecated.

This page is part of JavaScript String Methods. Related topics include sub() and small().

Understanding the sup() Method

Calling str.sup() does not create a live DOM node and does not take arguments. It concatenates an HTML string: <sup> + text + </sup>.

  • It is an instance method on strings (auto-boxed if needed).
  • It returns a new string — the original is unchanged.
  • No parameters — just str.sup().
  • Prefer createElement("sup") for real UI.

📝 Syntax

General form of String.prototype.sub:

JavaScript
str.sup()

Parameters

None.

Return value

A string beginning with a <sup> start tag, then the text of str, then a </sup> end tag.

Common patterns

JavaScript
"2".sup();
// '2'

// Prefer this in new code:
const elem = document.createElement("sup");
elem.textContent = "2";
document.body.appendChild(elem);

⚡ Quick Reference

GoalCode
Legacy HTML stringstr.sup()
Result shape<sup>...</sup>
Modern DOMdocument.createElement("sup")
Use in new apps?No — deprecated method

🔍 At a Glance

Four facts to remember about String.sup().

Returns
string

HTML markup text

Status
deprecated

Compatibility only

Tag
<sup>

Still valid HTML

Replace with
createElement

Real <sup> node

📋 sup() vs createElement("sup") vs substr()

str.sup()createElement("sup")substr()
PurposeHTML wrapperLive superscript elementExtract a substring
ResultHTML stringDOM elementString slice
Recommended?NoYesNo (prefer slice)
Best forReading legacy codeNew UI / formulasAvoid in new code

Examples Gallery

Examples follow MDN String.sup() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

See the exact HTML string sup() builds.

Example 1 — Basic sup()

MDN-style call that wraps text in <sup>.

JavaScript
const contentString = "Hello, world";
contentString.sup();
// 'Hello, world'
Try It Yourself

How It Works

The method returns markup text only. Nothing is added to the document until you assign it somewhere (for example innerHTML).

Example 2 — Empty String and Special Characters

Tags are still produced for empty text; content is not HTML-escaped.

JavaScript
"".sup();           // ''
"A < B".sup();      // 'A < B'
typeof "x".sup();   // "string"
Try It Yourself

How It Works

Unlike some attribute wrappers, sup() does not escape < inside the text. That is another reason to avoid injecting untrusted strings via innerHTML.

📈 Practical Patterns

Legacy injection patterns and the modern DOM replacement.

Example 3 — Injecting with innerHTML (Legacy)

How older demos used the returned string — MDN’s classic pattern.

JavaScript
const contentString = "Hello, world";
const html = contentString.sup();
// document.body.innerHTML = html;  // legacy pattern — avoid in new apps

console.log(html);
// Prefer createElement("sup") instead
Try It Yourself

How It Works

Browsers still understand <sup>, but building it as a string skips safer DOM APIs and makes XSS easier if content is untrusted.

Example 4 — Other HTML Wrappers Nearby

Recognize the same pattern family — all deprecated. Also shows a formula-style use.

JavaScript
"2".sup();      // '2'
"2".sub();      // '2'
"Hi".small();   // 'Hi'
"Hi".strike();  // 'Hi'

// Typical formula idea (legacy string build — avoid in new apps):
"x" + "2".sup();
// 'x2'
Try It Yourself

How It Works

These helpers return presentational HTML strings. For formulas today, build real <sup> / <sub> nodes (or use MathML) instead of concatenating tags.

Example 5 — Modern Replacement with createElement

MDN’s recommended approach — build a real <sup> node.

JavaScript
const contentString = "2";
const elem = document.createElement("sup");
elem.textContent = contentString;

// document.body.appendChild(elem);
console.log(elem.tagName);
console.log(elem.outerHTML);
Try It Yourself

How It Works

You get a live element, safer text assignment via textContent, and markup that matches modern HTML practice.

🚀 Common Use Cases

  • Reading legacy tutorials — recognize HTML wrapper methods in old samples.
  • Migrating old scripts — replace sup() with createElement("sup").
  • Exponents / footnotes — use a real <sup> element (not the string method).
  • Teaching string immutability — show that methods return new strings.
  • Not for new apps — do not use sup() to build superscript markup.
  • Not for extracting text — that is slice() / substring(), not sup().

🧠 How sup() Builds Markup

1

Start with text

You call str.sup() on a string receiver.

Input
2

Wrap with tags

Concatenate <sup> + text + </sup>.

Wrap
3

Return an HTML string

No DOM node is created — only text markup.

Result
4

Prefer the DOM instead

Create a real <sup> with createElement.

📝 Notes

  • sup() is deprecated — standardized only for compatibility.
  • The <sup> element is still valid HTML; the string method is not recommended.
  • Do not confuse sup() with substr() or substring().
  • It returns a string, not a DOM node.
  • It takes no parameters.
  • Avoid assigning untrusted strings to innerHTML.

Browser & Runtime Support

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

Deprecated · Legacy

String.sup()

Available in modern browsers for old code, but MDN recommends document.createElement("sub") instead.

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
sup() Avoid in new code

Bottom line: Recognize sup() in legacy samples. For new UI, create a real element with the DOM — never confuse it with substr().

Conclusion

String.prototype.sup() builds a <sup>...</sup> HTML string. It is useful for understanding history and migrating old code — not for writing new features.

Continue with at(), sub(), or the String methods hub.

💡 Best Practices

✅ Do

  • Use createElement("sup") for superscript text
  • Prefer textContent over string-built HTML
  • Replace sup() when you touch legacy files
  • Remember sup pairs with sub (both deprecated wrappers)
  • Learn wrappers only to recognize deprecated APIs

❌ Don’t

  • Use sup() in new production code
  • Confuse the valid <sup> element with the deprecated method
  • Inject untrusted sup() output via innerHTML
  • Expect sup() to return a live element
  • Mix HTML wrappers with modern component frameworks

Key Takeaways

Knowledge Unlocked

Five things to remember about String.sup()

Deprecated HTML wrapper — prefer createElement.

5
Core concepts
⚠️ 02

Status

deprecated

Legacy
03

Element

still valid

HTML
04

Replace

createElement

Modern
05

Pair

sub()

Name

❓ Frequently Asked Questions

String.prototype.sup() returns an HTML string that wraps the text in a <sup> element — for example "2".sup() returns '<sup>2</sup>'. It takes no parameters.
Yes. MDN marks all HTML wrapper methods as deprecated. Prefer document.createElement("sup") instead of building markup with string methods.
Yes. The <sup> element remains in HTML for superscript text (like x², ordinals, or footnotes). What is deprecated is the String.sup() helper that builds that markup as a string.
No. Strings are immutable. sup() returns a new string containing HTML markup. The original text stays the same.
Yes as a pair — sub() builds <sub> and sup() builds <sup>. Both are deprecated HTML wrappers. Prefer createElement("sub") / createElement("sup").
Use document.createElement("sup") and set textContent. Avoid injecting string-built HTML via innerHTML when you can create real nodes.
Did you know?

Pair sup() with sub() for old formula markup like x<sup>2</sup>. Both string helpers are deprecated — build real <sup> / <sub> nodes with createElement instead.

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

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