JavaScript String substring() Method
The string.substring() is an inbuilt function in JavaScript that is used to return the part of the given string from the start index to the end index. Indexing start from zero (0).
Syntax:
string.substring(Startindex, Endindex)
Parameters: Here the Startindex and Endindex describe the part of the string to be taken as a substring. Here the Endindex is optional.
Return value: It returns a new string that is part of the given string.
JavaScript code to show the working of string.substring() function:
Example 1: This example prints the values of the sub-strings from a variable string in the console.
javascript
<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 console.log(a); console.log(b); console.log(c); console.log(d);</script> |
Output:
geek eeksf forgeeks geeksforgeeks
Example 2: Index always starts with 0. If still, we take an index as negative, it will be considered zero and the index can’t be in fractions if it is found so, it will be converted into its just a lesser whole number.
javascript
<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 console.log(a); console.log(b); console.log(c);</script> |
Output:
geeksforgeeks eksforgeeks eksforgeeks
We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.
Supported Browser:
- 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

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.


Please Login to comment...