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?)
Call
What 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
Parameter
Description
start
Required. Begin index; negatives count from the end
length
Optional. How many characters; omit → to the end; negative → ""
Do not migrate blindly. Negative length makes substr and slice(a, a + l) disagree.
Compatibility
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.
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
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.