Description

The JavaScript String method substring() returns a specific part of a string between the start and end indices.

Syntax

The method substring() has the below syntax, where str is a string.

str.substring(startIndex, endIndex)

Parameters

The method substring() allows the below parameters.

startIndex

  • It is the index of the first character to include in the substring.

endIndex (optional)

  • It is the index of the character to stop including the characters.
  • The character with this index is excluded.
  • If this index is not provided, the method extracts till the end of the string.

Both the above parameters are processed as below.

  • If any index < 0, it is treated as 0.
  • If any index > str.length, it is treated as str.length.
  • If any index == NaN, it is treated as 0.
  • If startIndex > endIndex, the two arguments are swapped and processed.

Result

Returns a new string containing a specific part of a string based on start and end indices.

It doesn't change the original string.

Example 1: Using the Method

The below example shows the basic usage of the method.

var str = "Learning JavaScript is fun";

// Without indices
document.write(str.substring());    // Prints: Learning JavaScript is fun

// Using both start and end indices
document.write(str.substring(0, 1));     // Prints: L
document.write(str.substring(0, 5));     // Prints: Learn
document.write(str.substring(9, 19));    // Prints: JavaScript

// Using both start and end indices with same value
document.write(str.substring(9, 9));    // Prints an empty string

// Using only start index
document.write(str.substring(0));    // Prints: Learning JavaScript is fun
document.write(str.substring(9));    // Prints: JavaScript is fun

Output:

Learning JavaScript is fun
L
Learn
JavaScript

Learning JavaScript is fun
JavaScript is fun

Example 2: Using the Method with Negative Indices

If any of the index value is negative, the method converts such value to 0 (a zero) and executes.

var str = "Learning JavaScript is fun";

// Using one of the index negative
document.write(str.substring(-3, 5));    // Prints: Learn
document.write(str.substring(5, -3));    // Prints: Learn

// Using both indices negative
document.write(str.substring(-5, -5));   // Prints empty string

Output:

Learn
Learn
 

Example 3: Using the Method with NaN Indices

If any of the index values is NaN, the method converts such value to 0 (a zero) and executes.

var str = "Learning JavaScript is fun";

// Using one of the index NaN
document.write(str.substring(NaN, 5));    // Prints: Learn
document.write(str.substring(5, NaN));    // Prints: Learn

// Using both indices NaN
document.write(str.substring(NaN, NaN));  // Prints empty string

Output:

Learn
Learn
 

Example 4: Using the Method with Swapped Indices

If we need to check if a string contains a specific text at a specific index, we can use the position parameter.

var str = "Learning JavaScript is fun";

// Using startIndex < endIndex
document.write(str.substring(9, 19));    // Prints: JavaScript

// Using startIndex > endIndex
document.write(str.substring(19, 9));    // Prints: JavaScript

Output:

JavaScript
JavaScript

Overall

The JavaScript String method substring() returns a specific part of a string between the start and end indices.

Related Links