Substring
The .substring()
method returns part of a string. If given two arguments, they are the start and end indexes of the characters returned. If given one argument, it returns characters from that point to the end of the string.
Syntax
// Returns characters from startIndex to end of stringstring.substring(startIndex);// Returns characters from startIndex to endIndexstring.substring(startIndex, endIndex);
Details
.substring()
returns characters at the start index up to, but not including, the character at the end index.If the end index is omitted
.substring()
returns characters at the start index up through the end of the string.If the start and end indexes are equal,
.substring()
returns an empty string.Indexes less than zero are interpreted as zero.
Indexes that are
NaN
are treated as zero.Indexes that are greater than
string.length
are treated asstring.length
.If the first argument is greater than the second argument, the first argument is treated as the end index and the second argument is treated as the start index.
Example 1
Using .substring()
to display characters from a given string.
const str = 'Codecademy';console.log(str.substring(0, 4));console.log(str.substring(4, 0));console.log(str.substring(-4, 4));// Output: "Code"console.log(str.substring(4));console.log(str.substring(4, 99));// Output: cademy
Example 2
Using .substring()
to display the last 6
characters from a given string.
const str = 'Codecademy';console.log(str.substring(str.length - 6));// Output: cademy
Codebyte Example
Run the following code to understand how the .substring()
method works:
All contributors
- peterLundberg72876988671 total contribution
- christian.dinh2476 total contributions
- Anonymous contributorAnonymous contributor3071 total contributions
- StevenSwiniarski474 total contributions
- peterLundberg7287698867
- christian.dinh
- Anonymous contributor
- StevenSwiniarski
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.