extraction

String.prototype.substr

Returns a portion of the string starting at start with the specified length. Deprecated as a legacy web feature — new code should use slice or substring, but implementations still ship it.

str.substr(start[, length])

Parameters

Parameter Purpose
start Start index (negative counts from end)
length Number of characters to extract (default: to end)

Examples

console.log('hello world'.substr(6, 5));

Prints 'world'

console.log('hello'.substr(-3));

Prints 'llo'

console.log('hello'.substr(1, 3));

Prints 'ell'

console.log('hello'.substr(0, 0));

Prints '' (zero length)

Gotcha

DEPRECATED (Annex B legacy feature) — may be removed from future standards though currently supported everywhere. Second argument is a length, not an end index, unlike slice/substring.

Related methods

← All JS string methods · JS array methods