The substring() method is used to extract a portion of a string. It returns a new string without changing the original one.
- Extracts characters between two given indices.
- The ending index is not included in the result.
- The original string remains unchanged.
let text = "JavaScript";
let result = text.substring(0, 4);
console.log(result);
Syntax
string.substring(startIndex, endIndex);Parameters
- StartIndex: Describe the part of the string to be taken as a substring
- EndIndex: Describe the part of the string to be taken as a substring(optional).
Return value
- It returns a new string that is part of the given string.
let s = "Hello, World!";
// Extract substring from index 7 to index 12
let res = s.substring(7, 12);
console.log(res);
- Starting Index: The substring begins at index 7 ('W').
- Ending Index: The substring ends at index 12 (but does not include the character at index 12, which is '!').
- In this case, substring(7, 12) extracts the portion of the string from index 7 to index 11, resulting in "World".
Extracting Substrings by Character Index
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.
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.
let url = "https://www.geeksforgeeks.org/javascript";
let domain = url.substring(8, 29); // Extract the domain
let path = url.substring(29); // Extract the path
console.log(domain);
console.log(path);
String Validation
substring() can be used in string validation checks, such as checking whether a specific portion of a string matches a pattern.
let email = "[email protected]";
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.
let fName = "report.pdf";
let ext = fName.substring(fName.lastIndexOf('.') + 1);
console.log(ext);
Handling Negative Indices
The substring() method does not support negative indices; it converts them to 0, so extraction always starts from the beginning of the string rather than from the end.
- Negative values are treated as 0
- Does not count characters from the end
- Always starts extraction from the beginning when given negatives
let s = "Hello, World!";
let res = s.substring(-5, -1);
console.log(res);
When the Starting Index is Greater
If the start index is greater than the end index, substring() automatically swaps them, ensuring the correct substring is extracted without errors.
- Automatically swaps start and end indices
- Order of indices does not matter
- Prevents unexpected results or errors
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.
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.
let s1 = "I love coding";
let s2 = s1.substring(2, 6);
console.log(s1);
console.log(s2);
Use substring() in JavaScript
- Known Index Positions: Best when you already know the exact start and end indices to extract a fixed range of characters.
- Extracting Fixed Parts: Ideal for pulling static sections from strings like file names, URLs, or formatted user input.
- Uncertain Index Order: Automatically swaps indices if the start value is greater than the end value.
- Preserving Original String: Returns a new string without changing the original, making it safe for immutable operations.