Last Updated : 11 Jul, 2025
The substring() method in JavaScript is used to extract characters between two indices from a string, without modifying the original string. This method returns a new string that is a part of the original string, starting from a specified position up to (but not including) another specified position.
Syntax
string.substring(startIndex, endIndex);
Parameters
Return value
let s = "Hello, World!";
// Extract substring from index 7 to index 12
let res = s.substring(7, 12);
console.log(res);
One common use case for substring() is when you need to extract specific substrings from a known index range. For example, you might extract the first name or last name from a full name string.
JavaScript
let s1 = "Amit Ray";
let s2 = s1.substring(0, 4);
let s3 = s1.substring(5);
console.log(s2);
console.log(s3);
Extracting a Portion of a URL
You can use substring() to extract parts of a URL, such as the protocol, domain, or path.
JavaScript
let url = "https://www.geeksforgeeks.org/javascript/javascript-tutorial/";
let domain = url.substring(8, 29); // Extract the domain
let path = url.substring(29); // Extract the path
console.log(domain);
console.log(path);
www.geeksforgeeks.org /javascriptString Validation
substring() can be used in string validation checks, such as checking whether a specific portion of a string matches a pattern.
JavaScript
let email = "user@example.com";
let domain = email.substring(email.indexOf('@') + 1);
console.log(domain);
Removing a Prefix or Suffix
If you need to remove a prefix or suffix from a string, you can use substring() to extract the part of the string that remains.
JavaScript
let fName = "report.pdf";
let ext = fName.substring(fName.lastIndexOf('.') + 1);
console.log(ext);
Handling Negative Indices
Unlike some other methods, substring() treats negative indices as 0. It does not count from the end of the string. Instead, it converts negative values to 0, meaning the method starts from the beginning of the string.
JavaScript
let s = "Hello, World!";
let res = s.substring(-5, -1);
console.log(res);
This will result an empty string.
When the Starting Index is GreaterIf the starting index is greater than the ending index, the substring() method swaps the values of the indices internally. This makes it easy to extract the substring regardless of the order of indices.
JavaScript
let s = "Learning JavaScript";
let res = s.substring(13, 8);
console.log(res);
Using Only the Starting Index
If only the starting index is provided, substring() will return the substring from that index to the end of the string.
JavaScript
let s = "JavaScript is amazing!";
let res = s.substring(11);
console.log(res);
Immutability
Like most string methods in JavaScript, substring() does not alter the original string. Instead, it returns a new string.
JavaScript
let s1 = "I love coding";
let s2 = s1.substring(2, 6);
console.log(s1);
console.log(s2);
I love coding loveWhen to Use substring() in JavaScript
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4