String.prototype.sub() is a deprecated HTML wrapper method (same API as MDN String.prototype.sub()). It returns a string that wraps your text in a <sub> 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
Fundamentals
Introduction
Early JavaScript included helpers that wrapped text in HTML tags — sub(), sup(), bold(), italics(), and others. They made it easy to inject markup with innerHTML.
Today those wrappers are deprecated. sub() still works in browsers for compatibility. The <sub>element remains valid HTML (subscript for chemistry, 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 sub() so you can recognize legacy code. For new pages, use document.createElement("sub"). Do not confuse it with substr() (a different, legacy substring extractor).
You get a live element, safer text assignment via textContent, and markup that matches modern HTML practice.
Applications
🚀 Common Use Cases
Reading legacy tutorials — recognize HTML wrapper methods in old samples.
Migrating old scripts — replace sub() with createElement("sub").
Chemistry / footnotes — use a real <sub> element (not the string method).
Teaching string immutability — show that methods return new strings.
Not for new apps — do not use sub() to build subscript markup.
Not for extracting text — that is slice() / substring(), not sub().
🧠 How sub() Builds Markup
1
Start with text
You call str.sub() on a string receiver.
Input
2
Wrap with tags
Concatenate <sub> + text + </sub>.
Wrap
3
Return an HTML string
No DOM node is created — only text markup.
Result
4
⚠️
Prefer the DOM instead
Create a real <sub> with createElement.
Important
📝 Notes
sub() is deprecated — standardized only for compatibility.
The <sub>element is still valid HTML; the string method is not recommended.
Do not confuse sub() with substr() or substring().
It returns a string, not a DOM node.
It takes no parameters.
Avoid assigning untrusted strings to innerHTML.
Compatibility
Browser & Runtime Support
String.prototype.sub() is still widely implemented for compatibility, but it is deprecated. Do not use it in new projects.
✓ Deprecated · Legacy
String.sub()
Available in modern browsers for old code, but MDN recommends document.createElement("sub") instead.
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
sub()Avoid in new code
Bottom line: Recognize sub() in legacy samples. For new UI, create a real element with the DOM — never confuse it with substr().
Wrap Up
Conclusion
String.prototype.sub() builds a <sub>...</sub> HTML string. It is useful for understanding history and migrating old code — not for writing new features.
Confuse the valid <sub> element with the deprecated method
Inject untrusted sub() output via innerHTML
Expect sub() to return a live element
Mix HTML wrappers with modern component frameworks
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.sub()
Deprecated HTML wrapper — prefer createElement.
5
Core concepts
📝01
Returns
HTML string
API
⚠️02
Status
deprecated
Legacy
✅03
Element
still valid
HTML
✓04
Replace
createElement
Modern
⚡05
Not
substr()
Name
❓ Frequently Asked Questions
String.prototype.sub() returns an HTML string that wraps the text in a <sub> element — for example "2".sub() returns '<sub>2</sub>'. It takes no parameters.
Yes. MDN marks all HTML wrapper methods as deprecated. Prefer document.createElement("sub") instead of building markup with string methods.
Yes. The <sub> element remains in HTML for subscript text (like H₂O or footnotes). What is deprecated is the String.sub() helper that builds that markup as a string.
No. Strings are immutable. sub() returns a new string containing HTML markup. The original text stays the same.
No. sub() is a deprecated HTML wrapper. substr() is a legacy method that extracts part of a string by start index and length — prefer slice() instead of substr().
Use document.createElement("sub") and set textContent. Avoid injecting string-built HTML via innerHTML when you can create real nodes.
Did you know?
JavaScript has three similarly named APIs that confuse beginners: sub() (HTML wrapper), substr() (legacy extract by length), and substring() (extract by indexes). Only <sub> via the DOM is the modern way to render subscript — and slice() is preferred for cutting strings.