extraction

String.prototype.substring

Returns a new string between indexStart and indexEnd (exclusive). Unlike slice(), negative arguments are treated as 0 and the arguments are swapped when indexStart > indexEnd.

str.substring(indexStart[, indexEnd])

Parameters

Parameter Purpose
indexStart Start index; negative or NaN becomes 0
indexEnd Exclusive end index (default str.length)

Examples

console.log('hello world'.substring(0, 5));

Prints 'hello'

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

Prints 'el' (arguments swapped)

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

Prints 'hello' (negative treated as 0)

console.log('hello'.substring(2));

Prints 'llo'

Gotcha

Silent argument swapping and negative-as-zero behavior cause subtle bugs — prefer slice() unless you specifically want this coercion.

Related methods

← All JS string methods · JS array methods