JavaScript String substring()
Example
Extract characters from a string:
let str = "Hello world!";
str.substring(1, 4) // Returns "ell"
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The substring() method extracts characters, between to indices (positions), from a string,
and returns the substring.
The substring() method extracts characters between "start" and "end", not
including "end".
If "start" is greater than "end", substring() will swap the two arguments,
meaning (1, 4) equals (4, 1).
If "start" or "end" is less than 0, they are treated as 0.
The substring() method does not change the original string.
Browser Support
substring() is fully supported in all browsers:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | Yes | Yes | Yes | Yes | Yes |
Syntax
string.substring(start, end)
Parameter Values
| Parameter | Description |
|---|---|
| start | Required. The position where to start the extraction. First character is at index 0 |
| end | Optional. The position (up to, but not including) where to end the extraction. If omitted, it extracts the rest of the string |
Technical Details
| Return Value: | A new String containing the extracted characters |
|---|---|
| JavaScript Version: | ECMAScript 1 |
More Examples
Example
Begin the extraction at position 2, and extract the rest of the string:
str.substring(2);
Try it Yourself »
Example
If "start" is greater than "end", it will swap the two arguments:
str.substring(4, 1);
Try it Yourself »
Example
If "start" is less than 0, it will start extraction from index position 0:
str.substring(-3);
Try it Yourself »

