JavaScript String substr() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.substr(start, length?) returns a new string with length characters beginning at start. The second argument is a count, not an end index. Not the same as sub() (HTML wrapper) or substring() (two indexes).

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

Syntax

Call string.substr. Start with this line.

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

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

Library declarations
JavaScript
str.substr(start, length?)

Parameters

ParameterDescription
startRequired. Begin index; negatives count from the end
lengthOptional. How many characters; omit → to the end; negative → ""

Return Value

Return ValueDescription
*New string (original unchanged)

JavaScript Examples

Example 1 — Basic substr()

Extract by start index and optional length.

JavaScript
const str = "Mozilla";

console.log(str.substr(1, 2)); // "oz"
console.log(str.substr(2));    // "zilla"
Try It Yourself

How It Works

  1. Start at index 1. That character is "o".
  2. Take length 2. Characters "o" and "z" → "oz".
  3. Omit length. substr(2) copies from index 2 to the end → "zilla".
  4. Original stays put. str is still "Mozilla".

Example 2 — Negative Start and Length

Negative start counts from the end; negative length returns empty.

JavaScript
const str = "Mozilla";

console.log(str.substr(-3));      // "lla"
console.log(str.substr(-1, 1));   // "a"
console.log(str.substr(1, -1));   // ""
console.log(str.substr(-20, 2));  // "Mo"
console.log(str.substr(20, 2));   // ""
Try It Yourself

How It Works

  1. Negative start. -3 means three from the end → "lla".
  2. Negative length. substr(1, -1) returns "" immediately.
  3. Start clamped. substr(-20, 2) still begins at the start → "Mo".
  4. Past the end. substr(20, 2) has nothing to copy → "".

Example 3 — Prefer slice()

Same common cases with slice, plus the migration trap.

JavaScript
const str = "Mozilla";

// Common replacements
console.log(str.slice(1, 3)); // "oz"   was: substr(1, 2)
console.log(str.slice(2));    // "zilla" was: substr(2)
console.log(str.slice(-3));   // "lla"  was: substr(-3)

// Trap: negative length is NOT the same
const a = 1, l = -2;
const s = "01234";
console.log(s.substr(a, l));     // ""
console.log(s.slice(a, a + l));  // "123"
Try It Yourself

How It Works

  1. Length → end index. substr(1, 2) matches slice(1, 1 + 2) → slice(1, 3).
  2. Omit length. substr(i) becomes slice(i).
  3. Negative start. substr(-n) becomes slice(-n).
  4. Do not migrate blindly. Negative length makes substr and slice(a, a + l) disagree.

Browser & Runtime Support

String.prototype.substr() is still widely implemented for compatibility, but it is deprecated (ECMAScript Annex B). Prefer slice() for portable new code.

✓ Deprecated · Annex B

String.prototype.substr()

Available in major browsers and Node.js for legacy code. Not part of the main ECMAScript path — use slice() or substring() instead.

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

Bottom line: Recognize substr() when reading old code. For new extraction, use String.slice() with start/end indexes.

Frequently Asked Questions

substr(start, length?) returns a new string with length characters beginning at start. The original string is unchanged.
Yes. MDN marks it as deprecated. It lives in ECMAScript Annex B (web compatibility). Prefer String.prototype.slice() in new code.
In substr(), the second argument is a length (how many characters). In slice() and substring(), the second argument is an end index (exclusive).
A negative start counts from the end of the string — for example "Mozilla".substr(-3) returns "lla". Negative length returns an empty string "".
No. sub() is a deprecated HTML wrapper that builds <sub> markup. substr() extracts characters by start and length.
Not always. When length is negative they differ — substr returns "", while slice(a, a + l) may still return characters. Prefer slice with known non-negative lengths, or rewrite carefully.

Next: substring()

Extract between two indexes — and see how it differs from slice() and substr().

substring() 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.

10 people found this page helpful