The Wayback Machine - https://web.archive.org/web/20211215220840/https://www.geeksforgeeks.org/javascript-string-substring-method/amp/

JavaScript String substring() Method

The string.substring() is an inbuilt function in JavaScript which is used to return the part of the given string from start index to end index. Indexing start from zero (0). 
Syntax: 
 

string.substring(Startindex, Endindex)

Parameters: Here the Startindex and Endindex describes the part of the string to be taken as substring. Here the Endindex is optional. 
Return value: It returns a new string which is part of the given string. 
JavaScript code to show the working of string.substring() function: 
Code #1: 
 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!




<script>
 
    // Taking a string as variable
    var string = "geeksforgeeks";
    a = string.substring(0, 4)
    b = string.substring(1, 6)
    c = string.substring(5)
    d = string.substring(0)
 
    // Printing new string which are
    // the part of the given string
    document.write(a + "<br>");
    document.write(b + "<br>");
    document.write(c + "<br>");
    document.write(d + "<br>");
 
</script>                   

Output: 
 

geek
eeksf
forgeeks
geeksforgeeks

Code #2: 
Index always start with 0. If still we take index as negative, it will be considered as zero and index can’t be in fraction if it found so, it will be converted into its just lesser whole number. 
 






<script>
 
    // Taking a string as variable
    var string = "geeksforgeeks";
    a = string.substring(-1)
    b = string.substring(2.5)
    c = string.substring(2.9)
 
    // Printing new string which are
    // the part of the given string
    document.write(a + "<br>");
    document.write(b + "<br>");
    document.write(c + "<br>");
 
</script>                   

Output: 
 

geeksforgeeks
eksforgeeks
eksforgeeks

 

Supported Browser:




Article Tags :