String.prototype.substr()

Returns a portion of a string starting at an index for a given length (legacy).

Since ES3 (Annex B) Spec ↗

Syntax

str.substr(start, length)

Parameters

NameTypeRequiredDescription
start number Yes Index to start from. Negative counts from the end.
length number No Number of characters to extract. Defaults to the rest of the string.

Returns

string — A new string containing the extracted section.

Examples

console.log('hello'.substr(1, 3));
Output
ell
console.log('hello'.substr(-2));
Output
lo

Notes

Deprecated/legacy - kept only for web compatibility. Prefer `slice` or `substring` in new code. The second argument is a length, not an end index.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also