VOOZH about

URL: https://www.geeksforgeeks.org/javascript/javascript-string-substring-method/

⇱ JavaScript String substring() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JavaScript String substring() Method

Last Updated : 16 Jan, 2026

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.

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. 
  • 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.

Extracting a Portion of a URL

You can use substring() to extract parts of a URL, such as the protocol, domain, or path.

String Validation

substring() can be used in string validation checks, such as checking whether a specific portion of a string matches a pattern.

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.

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

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

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.

Immutability

Like most string methods in JavaScript, substring() does not alter the original string. Instead, it returns a new string.

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.
Comment