JavaScript String.prototype.substring() Explained with Examples

The substring() function extracts a sequence of characters from another given string. It does not alter the original string.

You define the sequence to extract with a start and end character index . These indexes are passed into the substring() function as parameters. The substring is formed from the character of the start index all the way to the character of the end index. Both indexes are counted from the beginning of the string, starting from 0 .

Examples:

"Hello, campers".substring(7, 14);
// output is "campers"

"Hello, world".substring(0, 5);
// output is "Hello"

You can also omit the last character index parameter, and the substring sequence will extract from the start index until the end of the string. Example:

"Hello, campers!".substring(7);
// output is "campers!"