String.prototype.sub() is a legacy HTML wrapper that returns a new string wrapping your text in <sub>…</sub>. It takes no parameters and never mutates the original string. Not the same as substr() (which extracts text).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call string.sub. Start with this line.
JavaScript
string.sub()
Call
What it does
string.sub()
Primary signature.
string.sub(...)
Call with the arguments your case needs.
Built into JavaScript. Call sub(...) on a string (or String instance) — no import required.
Library declarations
JavaScript
string.sub()
Parameters
This member has no parameters beyond those shown in the signature.
Return Value
Return Value
Description
*
HTML string with <sub> tags
JavaScript Examples
Example 1 — Basic Wrap
Call sub() to build the classic <sub> HTML string.
Empty input."".sub() still wraps tags around nothing → "<sub></sub>".
No escaping."A < B" keeps the < character inside the markup.
Type is string. You get text, not a live DOM element.
Safety tip. Never feed untrusted input through sub() into innerHTML.
Example 3 — Prefer DOM Instead
Compare the legacy wrapper with modern createElement("sub").
JavaScript
// Legacy — do not use in new code
console.log("2".sub());
// "<sub>2</sub>"
// Prefer a real element
const el = document.createElement("sub");
el.textContent = "2";
console.log(el.outerHTML);
// "<sub>2</sub>"
// Not the same as extracting text
console.log("hello".substr(1, 3)); // "ell"
console.log("hello".sub()); // "<sub>hello</sub>"
Legacy path."2".sub() builds an HTML string via the deprecated method.
Create a real element.createElement("sub") + textContent is the safe DOM approach.
Name trap.substr() extracts characters; sub() wraps HTML.
Rule of thumb. Learn sub() to read old code; ship DOM nodes for new UI.
Compatibility
Browser & Runtime Support
String.prototype.sub() is still implemented widely for compatibility, but it is deprecated. Do not use it in new projects.
✓ Deprecated · Legacy
String.prototype.sub()
Available in major browsers and Node.js for legacy compatibility. Prefer document.createElement("sub") in new code.
LegacyCompatibility only
Google ChromeSupported · Legacy API
Legacy support
Mozilla FirefoxSupported · Legacy API
Legacy support
Apple SafariSupported · Legacy API
Legacy support
Microsoft EdgeSupported · Legacy API
Legacy support
Internet ExplorerSupported · Legacy
Legacy support
OperaSupported · Legacy API
Legacy support
Samsung InternetSupported · Android
Legacy support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Legacy support
sub()Avoid in new code
Bottom line: Recognize sub() when reading old code. For new UI, use createElement("sub") — and do not confuse it with substr().
Frequently Asked Questions
sub() returns a new HTML string that wraps the text in a <sub> element — for example "2".sub() returns "<sub>2</sub>". It takes no parameters and does not change the original string.
Yes. MDN marks all HTML wrapper methods as deprecated. Prefer document.createElement("sub") and set textContent instead of building markup with string methods.
Yes. Unlike <strike> or <blink>, <sub> remains in HTML for subscript text (for example chemical formulas 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() extracts part of a string by start index and length — prefer slice() instead of substr().
Use document.createElement("sub"), set textContent, and append the element. Avoid injecting string-built HTML via innerHTML when you can create real nodes.