JavaScript String substr()
Syntax & Examples


String.substr() method

The substr() method of the String class in JavaScript returns a portion of the string, starting at the specified index and extending for a given number of characters afterwards.


Syntax of String.substr()

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

1.
substr(start)

Parameters

ParameterOptional/RequiredDescription
startrequiredThe index of the first character to include in the returned substring. A negative index counts back from the end of the string.

This method returns a portion of the string starting from the specified index to the end of the string.

Returns value of type String.

2.
substr(start, length)

Parameters

ParameterOptional/RequiredDescription
startrequiredThe index of the first character to include in the returned substring. A negative index counts back from the end of the string.
lengthoptionalThe number of characters to include in the returned substring. If omitted, it extracts characters to the end of the string.

This method returns a portion of the string starting from the specified index and extending for the specified number of characters.

Returns value of type String.



✐ Examples

1 Using substr() method with only the start index

In JavaScript, we can use the substr() 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 substr() 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.substr(7);
console.log(subStr);

Output

'world!'

2 Using substr() method with start index and length

In this example, we use the substr() method to extract a substring starting from the specified index and extending for the specified number of characters.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the substr() method with the start index 7 and the length 5.
  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.substr(7, 5);
console.log(subStr);

Output

'world'

3 Using substr() method with a negative start index

This example demonstrates how to use the substr() method with a negative start index to extract a substring from the end of the string.

For example,

  1. We define a string variable str with the value 'Hello, world!'.
  2. We use the substr() method with the start index -6 to extract the last six characters.
  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.substr(-6);
console.log(subStr);

Output

'world!'

Summary

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