The Wayback Machine - https://web.archive.org/web/20240205133712/https://www.geeksforgeeks.org/javascript-string-substr-method/
Open In App
Related Articles

JavaScript String substr() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

JavaScript str.substr() method returns the specified number of characters from the specified index from the given string. It basically extracts a part of the original string.

Syntax:

str.substr(start , length)

Parameters:

  • start: It defines the starting index from where the substring is to be extracted from the base string.
  • length: It defines the number of characters to be extracted starting from the start in the given string. If the second argument to the method is undefined then all the characters from the start till the end of the length are extracted.

Return value: This method 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.

Examples of the above method are provided below:

Example 1: 

JavaScript




// 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: In this example, the method substr() creates a substring starting from index 5 till the end of the string.

 a great day.

Example 2: In this example, the method substr() extracts the substring starting at index 5, and the length of the string is 6.

JavaScript




// JavaScript to illustrate substr() method
 
function func() {
 
    // Original string
    let str = 'It is a great day.';
 
    let sub_str = str.substr(5, 6);
    console.log(sub_str);
}
 
func();


Output: 

 a gre

Example 3: 

JavaScript




// 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: In this example since the length of the string to be extracted is negative therefore the method returns an empty string.

 ""

Example 4: In this example, the method substr() extracts the substring starting at index 7 from the end of the array, and the length of the string is 6.

Javascript




// 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: In this example, the method substr() creates a substring starting from index 7 from the end of the string till the end of the string.

at day

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

Supported Browsers:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 3 and above
  • Opera 3 and above
  • Safari 1 and above

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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 22 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials