Description

The JavaScript String method substr() returns a specific part of a string based on the specified start index and length.

  • Extracts a part of a string, beginning from the specified start index, and returns the specified number of characters.
  • It doesn't change the original string.
  • To extract characters from the end of a string, we can use a negative start index.

Syntax

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

str.substr(startIndex, length)

Parameters

The method substr() allows the below parameters.

startIndex

  • It is the index of the first character to include in the substring.
  • If it is greater than the string length, the method returns an empty string.
  • If it is negative, the method counts from the end of the string.

length (optional)

  • It determines the number of characters to extract.
  • If omitted, the method extracts till the end of the string.
  • If the length is zero or negative, the method returns an empty string.

Result

Returns a new string containing a specific part of a string based on start index and length.

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 parameters
document.write(str.substr());    // Prints: Learning JavaScript is fun

// Using both start index and length parameters
document.write(str.substr(0, 1));     // Prints: L
document.write(str.substr(0, 5));     // Prints: Learn
document.write(str.substr(9, 10));    // Prints: JavaScript

// Using only start index
document.write(str.substr(0));    // Prints: Learning JavaScript is fun
document.write(str.substr(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 Start Index

The negative start index counts the characters from the end of the string, where the index -1 represents the last character, -2 represents the 2nd character from the last, and so on.

var str = "Learning JavaScript is fun";

// Using one of the index negative
document.write(str.substr(-3, 3));    // Prints: fun
document.write(str.substr(-6));    // Prints: is fun

Output:

fun
is fun

Example 3: Using the Method with Start Index > String Length

If the start index is bigger than the string length, the method returns an empty string.

var str = "Learning JavaScript is fun";

// Using start index greater than string length
document.write(str.substr(100, 5));    // Prints: Learn
document.write(str.substr(Infinity, 5));    // Prints: Learn

Output:

 
 

Example 4: Using the Method with Start Index = NaN

If we use NaN for the start index, the method converts NaN to 0 (a zero value) and executes it.

var str = "Learning JavaScript is fun";

// Using NaN for start index
document.write(str.substr(NaN, 5));    // Prints: Learn

Output:

Learn

Example 5: Using the Method with Length <= 0

If the length value is less than or equal to zero, the method returns an empty string.

var str = "Learning JavaScript is fun";

// Using length <= 0
document.write(str.substr(9, 0));    // Prints empty string
document.write(str.substr(9, -4));    // Prints empty string

// Using length > 0
document.write(str.substr(9, 4));    // Prints: Java

Output:

 
 
Java

Overall

The JavaScript String method substr() returns a specific part of a string based on the specified start index and length.

Related Links