String.prototype.substring()
Returns the part of a string between two indices.
Syntax
str.substring(start, end) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
start | number | Yes | Index of the first character to include. |
end | number | No | Index of the first character to exclude. Defaults to string length. |
Returns
string — A new string containing the specified part.
Examples
console.log('hello'.substring(1, 4));
Output
ell
console.log('hello'.substring(4, 1));
Output
ell
Notes
Swaps the arguments if start is greater than end, and treats negative or NaN
values as 0. For negative-index support use `slice` instead.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 1.0 |
| safari | 1.0 |
| edge | 12 |
| node | 0.10 |