String.prototype..substring()
string.substring(start[, end]) string · Added in vES1 · Updated March 13, 2026 · String Methods The substring() method returns a portion of a string between the start index and the character before the end index. It does not modify the original string—instead, it creates and returns a new string.
Syntax
str.substring(startIndex)
str.substring(startIndex, endIndex)
Parameters
| Parameter | Type | Description |
|---|---|---|
startIndex | number | The index where extraction begins. Characters at this index are included. |
endIndex | number | Optional. The index where extraction ends. Characters at this index are excluded. |
Return Value
A new string containing the extracted portion. If endIndex is omitted, extraction continues to the end of the string.
Description
The substring() method swaps its arguments if startIndex is greater than endIndex. It treats any value that is NaN or negative as 0. If either index exceeds the string length, they are treated as the string length.
Unlike slice(), substring() does not accept negative indices. For negative index support, use slice() instead.
Examples
Basic Usage
const str = "Hello, World!";
console.log(str.substring(0, 5)); // "Hello"
console.log(str.substring(7)); // "World!"
console.log(str.substring(7, 12)); // "World"
Swapping Arguments
If startIndex is greater than endIndex, the arguments are automatically swapped:
const str = "Hello";
console.log(str.substring(5, 0)); // "Hello" — same as substring(0, 5)
console.log(str.substring(2, 5)); // "llo"
Out of Bounds Indices
Indices beyond the string length are clamped:
const str = "Hello";
console.log(str.substring(0, 100)); // "Hello" — endIndex clamped to string length
console.log(str.substring(5)); // "" — empty string, startIndex equals length
console.log(str.substring(10)); // "" — empty string, startIndex exceeds length
Negative and NaN Indices
Negative indices and NaN are treated as 0:
const str = "Hello";
console.log(str.substring(-3, 2)); // "He" — -3 treated as 0
console.log(str.substring(2, NaN)); // "He" — NaN treated as 0
console.log(str.substring("abc")); // "Hello" — NaN from string conversion
Extracting a Single Character
To extract a single character, use the same index for both parameters:
const str = "Hello";
console.log(str.substring(1, 2)); // "e"
Common Use Cases
Removing a Prefix or Suffix
const url = "https://example.com";
const domain = url.substring(8); // "example.com"
const text = "Hello, World!";
const trimmed = text.substring(0, 5); // "Hello"
Extracting Between Delimiters
const email = "user@example.com";
const username = email.substring(0, email.indexOf("@")); // "user"
Performance Notes
substring() is fast and widely supported across all JavaScript environments. For most substring extraction tasks, it gets the job done efficiently.
See Also
- String.slice() — Extracts a portion using start/end indices, supports negative indices
- String.split() — Splits a string into an array based on a delimiter
- String.lastIndexOf() — Returns the last index of a specified value