JavaScript String substring()
Syntax & Examples


String.substring() method

The substring() method of the String class in JavaScript returns a new string containing characters of the calling string from (or between) the specified index (or indices).


Syntax of String.substring()

There are 2 variations for the syntax of String.substring() method. They are:

1.
substring(indexStart)

Parameters

ParameterOptional/RequiredDescription
indexStartrequiredThe index of the first character to include in the returned substring. If greater than indexEnd, the two indexes are swapped.

This method returns a new string containing characters from the specified index to the end of the string.

Returns value of type String.

2.
substring(indexStart, indexEnd)

Parameters

ParameterOptional/RequiredDescription
indexStartrequiredThe index of the first character to include in the returned substring. If greater than indexEnd, the two indexes are swapped.
indexEndoptionalThe index of the first character to exclude from the returned substring. If omitted, extracts characters to the end of the string.

This method returns a new string containing characters from the specified start index up to, but not including, the specified end index.

Returns value of type String.



✐ Examples

1 Using substring() method with only the start index

In JavaScript, we can use the substring() method to extract a substring starting from the specified index to the end of the string.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the substring() method with the start index 7.
  3. The extracted substring, 'world!', is stored in the variable subStr.
  4. We log subStr to the console using console.log() method.

JavaScript Program

const str = 'Hello, world!';
const subStr = str.substring(7);
console.log(subStr);

Output

'world!'

2 Using substring() method with start and end indices

In this example, we use the substring() method to extract a substring starting from the specified index and ending at the specified index.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the substring() method with the start index 7 and the end index 12.
  3. The extracted substring, 'world', is stored in the variable subStr.
  4. We log subStr to the console using console.log() method.

JavaScript Program

const str = 'Hello, world!';
const subStr = str.substring(7, 12);
console.log(subStr);

Output

'world'

3 Using substring() method with indices in reverse order

This example demonstrates how the substring() method swaps the indices if the start index is greater than the end index.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the substring() method with the start index 12 and the end index 7.
  3. The extracted substring, 'world', is stored in the variable subStr.
  4. We log subStr to the console using console.log() method.

JavaScript Program

const str = 'Hello, world!';
const subStr = str.substring(12, 7);
console.log(subStr);

Output

'world'

Summary

In this JavaScript tutorial, we learned about substring() method of String: the syntax and few working examples with output and detailed explanation for each example.