JavaScript String substring() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.substring(indexStart, indexEnd?) returns a new string from indexStart up to (but not including) indexEnd. It never mutates the original string.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.substring. Start with this line.

JavaScript
str.substring(start, end?)
CallWhat it does
str.substring(start, end?)Primary signature.
string.substring(...)Call with the arguments your case needs.

Built into JavaScript. Call substring(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
str.substring(start, end?)

Parameters

ParameterDescription
indexStartRequired. Where the copy begins
indexEndOptional. Exclusive stop; omit → end of string

Return Value

Return ValueDescription
*New string (original unchanged)

JavaScript Examples

Example 1 — Extract with Start and End

Copy a piece of a string. The end index is exclusive.

JavaScript
const text = "Mozilla";

console.log(text.substring(0, 3));  // "Moz"
console.log(text.substring(3));     // "illa"
console.log(text.substring(3, 7));  // "illa"
console.log(text);                  // "Mozilla" (unchanged)
Try It Yourself

How It Works

  1. Pick a range. substring(0, 3) copies indexes 0, 1, 2.
  2. End is exclusive. The character at index 3 ("i") is not included in the first call.
  3. Omit the end. substring(3) copies from index 3 to the end.
  4. No mutation. The original text stays "Mozilla".

Example 2 — Swapped Args and Negatives

When start is greater than end, substring swaps them. Negatives become 0.

JavaScript
const text = "Mozilla";

console.log(text.substring(3, 0));   // "Moz" (swapped)
console.log(text.substring(-2, 3));  // "Moz" (-2 → 0)
console.log(text.substring(text.length - 2)); // "la"
Try It Yourself

How It Works

  1. Swap rule. substring(3, 0) becomes substring(0, 3) → "Moz".
  2. Negatives clamp to zero. -2 is not “two from the end”.
  3. From-the-end manually. Use length - 2 if you stay with substring.
  4. Prefer slice for negatives. slice(-2) is clearer for that case.

Example 3 — substring() vs slice()

Same results for simple ranges; different rules for negatives and swapped args.

JavaScript
const text = "Mozilla";

console.log(text.substring(0, 3));  // "Moz"
console.log(text.slice(0, 3));      // "Moz"

console.log(text.substring(3, 0));  // "Moz" (swap)
console.log(text.slice(3, 0));      // ""    (no swap)

console.log(text.substring(-2));    // "Mozilla" (-2 → 0)
console.log(text.slice(-2));        // "la"
Try It Yourself

How It Works

  1. Happy path matches. Non-negative start < end is the same for both.
  2. Swap differs. substring reorders; slice returns "".
  3. Negatives differ. substring clamps to 0; slice counts from the end.
  4. Rule of thumb. Prefer slice for new code unless you need the swap behavior.

Browser & Runtime Support

String.prototype.substring() is a long-standing part of JavaScript. It works in all browsers and Node.js versions you are likely to target.

✓ Baseline · Widely available

String.prototype.substring()

Supported everywhere JavaScript strings are — including legacy browsers. No polyfill needed for normal web or Node targets.

100% Universal support
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
substring() Excellent

Bottom line: Safe in every environment that runs JavaScript. For negative-index extracts, prefer slice() for clearer intent.

Frequently Asked Questions

String.prototype.substring(indexStart, indexEnd?) returns a new string from indexStart up to (but not including) indexEnd. Omit indexEnd to go to the end of the string.
No. indexEnd is exclusive — the character at indexEnd is not included. For example "Mozilla".substring(0, 3) returns "Moz".
substring() swaps the two arguments. So "Mozilla".substring(3, 0) is the same as "Mozilla".substring(0, 3). slice() would return "" instead.
Unlike slice(), substring() treats negative values (and NaN) as 0. It does not count from the end of the string.
substring(start, end) uses two indexes. substr(start, length) uses a length (and is deprecated). Prefer substring or slice over substr.
Both are fine for non-negative indexes. Prefer slice() when you need negative indexes from the end. Prefer substring() only if you rely on its swap-when-start-greater-than-end behavior.

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.

11 people found this page helpful