JavaScript substring() mehtod


The substring() method in JavaScript is used to extract a portion of a string between two specified indices. This method returns a new string that contains the characters from the original string, starting at the specified startIndex and ending just before the specified endIndex.

Syntax:

string.substring(startIndex, endIndex)
  • startIndex: The index at which to begin extraction (inclusive). If this index is greater than the length of the string, it is treated as equal to the string's length.
  • endIndex (optional): The index at which to end extraction (exclusive). If omitted, substring() extracts characters to the end of the string. If this index is greater than the length of the string, it is treated as equal to the string's length.

Return Value:

  • Returns a new string that contains the extracted portion of the original string. The original string remains unchanged.

Example 1: Basic Usage

let str = "Hello, World!"; let result = str.substring(7, 12); console.log(result); // "World"

In this example, substring(7, 12) extracts the characters from index 7 to index 12, resulting in the substring "World".

Example 2: Omitted End Index

If the endIndex is omitted, substring() will extract characters from the startIndex to the end of the string.

let str = "Hello, World!"; let result = str.substring(7); console.log(result); // "World!"

Here, substring(7) extracts the substring starting from index 7 to the end of the string.

Example 3: Handling Indices Out of Range

If startIndex is greater than endIndex, substring() will swap the two indices.

let str = "Hello, World!"; let result = str.substring(12, 7); console.log(result); // "World"

In this case, since startIndex is 12 and endIndex is 7, the method swaps them and effectively behaves like substring(7, 12).

Example 4: Negative Indices

If either index is negative, it is treated as 0. This means that negative indices do not have the same behavior as they do with the slice() method.

let str = "Hello, World!"; let result = str.substring(-5, 5); console.log(result); // "Hello"

Here, the negative startIndex is treated as 0, so the method extracts characters from the beginning of the string.

Example 5: Start Index Greater Than Length

If startIndex is greater than or equal to the string's length, the method will return an empty string.

let str = "Hello, World!"; let result = str.substring(15); console.log(result); // ""

In this case, since the startIndex is 15, which is greater than the length of the string, the method returns an empty string.

Example 6: Original String Remains Unchanged

The original string is not modified by the substring() method.

let str = "Hello, World!"; let result = str.substring(0, 5); console.log(str); // "Hello, World!" (original string remains unchanged) console.log(result); // "Hello"

Summary:

  • The substring() method extracts a portion of a string between two specified indices.
  • It takes two parameters: startIndex (inclusive) and endIndex (exclusive).
  • If the endIndex is omitted, the method extracts to the end of the string.
  • If startIndex is greater than endIndex, the indices are swapped.
  • Negative indices are treated as 0, and indices out of range are adjusted accordingly.
  • The method returns a new string without modifying the original string.