JavaScript String.prototype.substring() Method
The substring() method returns the part of a string between two indexes. Unlike slice(), it doesn't accept negative indexes.
Description
The substring() method returns the part of the string between the start and end indexes. It differs from slice() in two ways: negative values are treated as 0, and if start is greater than end, the two are swapped. Because those rules can hide mistakes, slice() is usually the better choice.
If either argument is NaN, it's treated as 0, and values larger than the string's length are treated as the length. With only one argument, it returns everything from that index to the end, like slice() with one argument.
The swapping behavior can be useful when you have two positions and don't know which comes first, but in most code it hides mistakes. There's also an older, deprecated substr() method, which takes a start and a length; avoid it in new code and use slice() instead.
Syntax
string.substring(start)
string.substring(start, end)Parameters
- startThe index to start at.
- endOptional. The index to stop before.
Return Value
A new string containing the extracted part.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.substring() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





