Open In App

JavaScript String substr() Method

Last Updated : 22 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The substr() method in JavaScript extracts a portion of a string, starting from a specified index position and extending for a given number of characters.

This method was traditionally used to extract a part of a string, however, the method is now considered outdated, and the substring() or slice() methods are recommended as alternatives.

Syntax:

str.substr(start, length)

Parameters:

  • start: The index where the extraction begins.
  • length (optional): The number of characters to extract. If omitted, it extracts the rest of the string from the start index.

Return value:

Returns a string that is part of the given string. If the length is 0 or a negative value then it returns an empty string. If we want to extract the string from the end then use a negative start position.

Example 1: Extract Substring using substr()

This code demonstrates the usage of the substr() method in JavaScript. It extracts a substring from the original string starting from the specified index (5 in this case) till the end. The extracted substring is then printed.

// JavaScript to illustrate substr() method
function func() {

    // Original string
    let str = 'It is a great day.';
    let sub_str = str.substr(5);
    console.log(sub_str);
}

func();

Output
 a great day.

Example 2: Negative Length in substr() Method

This code demonstrates an attempt to use a negative length parameter in the substr() method, which is invalid. JavaScript substr() method expects a positive length value, resulting in an empty string when provided with a negative value.

// JavaScript to illustrate substr() method
function func() {

    // Original string
    let str = 'It is a great day.';

    let sub_str = str.substr(5, -7);
    console.log(sub_str);
}
func();

Output

Example 3: Extracting Substring from End Using substr()

This code snippet utilizes the substr() method to extract a substring from the end of the original string ‘It is a great day.’. The negative index -7 indicates starting from the 7th character from the end, and 6 characters are extracted.

// JavaScript to illustrate substr() method
function func() {

    // Original string
    let str = 'It is a great day.';

    let sub_str = str.substr(-7, 6);
    console.log(sub_str);
}

func();

Output
at day

Now to use modern methods like substring() or slice() you can follow these articles.

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

JavaScript String substr() Method – FAQ’s

What does the substr() method do in JavaScript?

The substr() method extracts a portion of a string, starting from a specified index position, and returns a new string with a specified number of characters.

Does substr() modify the original string?

No, substr() does not modify the original string. It returns a new string containing the extracted characters, leaving the original string unchanged.

Can you use negative numbers with substr()?

Yes, you can use negative numbers with substr(). A negative start index indicates the position from the end of the string. For example, str.substr(-3, 2) extracts 2 characters starting from the 3rd character from the end.

How does substr() handle out-of-bounds indices?

If the start index provided to substr() is greater than or equal to the string’s length, it returns an empty string. If the length parameter makes the extraction extend beyond the string’s length, it extracts characters until the end of the string.

Are there scenarios where substr() might be less efficient?

substr() may be less efficient when used repeatedly in loops or when extracting very large portions of a string. In such cases, using techniques like caching results or using more efficient string manipulation methods might improve overall performance.

We have a Cheat Sheet on JavaScript where we covered all the important topics of JavaScript to check those please go through JavaScript Cheat Sheet – A Basic Guide to JavaScript.



Next Article

Similar Reads

three90RightbarBannerImg