Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.substring

Returns the part of a string between two positions.

Available since: ES1

Syntax

str.substring(indexStart, indexEnd)

Parameters

ParameterTypeDescription
indexStartnumberThe position of the first character, counting from zero.
indexEndnumberOptional. The position to stop before. To the end of the string by default.

Return value

stringA new string with the selected characters.

Examples

Characters two to five:

console.log('JavaScript'.substring(0, 4));
console.log('JavaScript'.substring(4));

Output:

Java
Script

The difference from slice() — negative numbers are treated as zero:

console.log('JavaScript'.substring(-6));
console.log('JavaScript'.slice(-6));

Output:

JavaScript
Script

Notes

If the first argument is greater than the second, substring() swaps them. slice() behaves more predictably, so new code usually uses it.

See also

In the tutorial

  • String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.

Thanks for reading!

Donate to the site — we work hard for you.