The substring method

The substring method is used to get substring of a given string by specifying start and end numbers. The starting index is the number of character in the given string that will be included to get substring. Whereas end number will not be included in substring, javascript method.

substring example with both indices

Following example uses both indices in substring method. We will specify start and end index to a string variable and assign it to another string variable. As you click on the button “Show Substring” it will show substring value.

As you can see, we just given starting index 5 and remaining substring is returned as “is javascript string tutorial!”.

Javascript string substring example with greater end index

As mentioned earlier, when given end index in string substring method is greater than the start index then the order will be inverted, i.e. end index becomes starting index while start value becomes end index. See example by clicking the link below: