JavaScript Substring
The .substring()
method in JavaScript extracts a portion of a string from one position to another (exclusive) and returns a new string. If the second position is omitted, it returns characters from the first position to the end of the string.
Syntax
string.substring(startIndex, endIndex);
Parameters:
startIndex
: The position where extraction begins.endIndex
(Optional): The position before which to end extraction. If omitted,.substring()
returns characters fromstartIndex
to the end of the string.
Return value:
The .substring()
method returns a newly created string containing the substring extracted from the input string.
Notes:
- If
startIndex
andendIndex
are equal,.substring()
returns an empty string.- Indices that are greater than
string.length
are treated asstring.length
.- If
startIndex
is greater thanendIndex
, thenstartIndex
is treated as the end index andendIndex
is treated as the start index.
Example 1: Basic Usage of .substring()
This example uses the .substring()
method to extract characters from index 0
to index 4
(exclusive) in the str
string:
let str = 'JavaScript';let result = str.substring(0, 4);console.log(result);
Here is the output:
Java
Example 2: Using .substring()
Without endIndex
This example uses the .substring()
method without the endIndex
parameter to extract characters from index 9
to the end of the str
string:
let str = 'Frontend Developer';let result = str.substring(9);console.log(result);
Here is the output:
Developer
Codebyte Example: Using .substring()
with Reversed Indices
This codebyte example uses the .substring()
method with the startIndex
parameter being greater than endIndex
to extract characters from index 0
to index 6
in the str
string:
Frequently Asked Questions
1. What happens if startIndex
or endIndex
in .substring()
is negative?
If startIndex
or endIndex
in .substring()
is negative, it is treated as 0
.
2. How is .substring()
different from .slice()
?
.substring()
swaps the indices if the first is greater than the second, unlike.slice()
..slice()
supports negative indices, unlike.substring()
.
3. Can I use .substring()
on string literals?
Yes, you can use .substring()
on string literals because JavaScript treats them as temporary String
objects, making them eligible for calling methods on.
All contributors
- Anonymous contributor
- StevenSwiniarski
- Sriparno08
- Anonymous contributor
- peterLundberg7287698867
- christian.dinh
Contribute to Docs
- 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.
Learn JavaScript on Codecademy
- Skill path
Create a Back-End App with JavaScript
Learn how to build back-end web APIs using Express.js, Node.js, SQL, and a Node.js-SQLite database library.Includes 8 CoursesWith CertificateBeginner Friendly30 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours