JavaScript String sub() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

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()
CallWhat 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 ValueDescription
*HTML string with <sub> tags

JavaScript Examples

Example 1 — Basic Wrap

Call sub() to build the classic <sub> HTML string.

JavaScript
const digit = "2";

console.log(digit.sub());
// "<sub>2</sub>"

console.log("H" + digit.sub() + "O");
// "H<sub>2</sub>O"
Try It Yourself

How It Works

  1. Start with text. "2" is a normal string.
  2. Call sub(). No arguments — the method wraps the whole string.
  3. Get HTML markup. The return value is "<sub>2</sub>".
  4. Join for formulas. Concatenate around the wrapper to form strings like water’s formula markup.

Example 2 — Empty String & Special Characters

See empty wrappers and that < is not escaped.

JavaScript
console.log("".sub());       // "<sub></sub>"
console.log("A < B".sub());  // "<sub>A < B</sub>"
console.log(typeof "x".sub()); // "string"
Try It Yourself

How It Works

  1. Empty input. "".sub() still wraps tags around nothing → "<sub></sub>".
  2. No escaping. "A < B" keeps the < character inside the markup.
  3. Type is string. You get text, not a live DOM element.
  4. 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>"
Try It Yourself

How It Works

  1. Legacy path. "2".sub() builds an HTML string via the deprecated method.
  2. Create a real element. createElement("sub") + textContent is the safe DOM approach.
  3. Name trap. substr() extracts characters; sub() wraps HTML.
  4. Rule of thumb. Learn sub() to read old code; ship DOM nodes for new UI.

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.

Legacy Compatibility only
Google Chrome Supported · Legacy API
Legacy support
Mozilla Firefox Supported · Legacy API
Legacy support
Apple Safari Supported · Legacy API
Legacy support
Microsoft Edge Supported · Legacy API
Legacy support
Internet Explorer Supported · Legacy
Legacy support
Opera Supported · Legacy API
Legacy support
Samsung Internet Supported · Android
Legacy support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

Next: sup()

Another deprecated HTML wrapper — this one builds <sup> markup from a string.

sup() tutorial →

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.

7 people found this page helpful