Home »
JavaScript
String substring() Method with Example in JavaScript
JavaScript String substring() Method: Here, we are going to learn about the substring() method of the string in JavaScript with Example.
Submitted by Shivang Yadav, on February 23, 2020
JavaScript | String substring() Method
The String.substring() method in JavaScript is used to return a part of the string. The substring() extracted is from the given start and end index of the string.
Syntax:
string.substring(startIndex, endIndex);
Parameter(s):
The method accepts two parameters, they are the starting and ending index of the substring string which is to be extracted.
Return value:
The return type of this method is string which is the substring output of the given string.
Browser support: Chrome, Internet explorer, Mozilla, Safari, Opera mini.
Example 1:
<script>
document.write("Hello".substring(1,3) + "<br/>");
document.write("Hello".substring(0,2) + "<br/>");
document.write("Hello".substring(0,5) + "<br/>");
document.write("Hello".substring(0,8) + "<br/>"); //no error
document.write("Hello World!".substring(6,12) + "<br/>");
</script>
Output
el
He
Hello
Hello
World!
Example 2:
<script>
str = "www.includehelp.com";
document.write(str.substring(0,3) + "<br/>");
document.write(str.substring(4,15) + "<br/>");
document.write(str.substring(16,19) + "<br/>");
document.write(str.substring(0) + "<br/>");
document.write(str.substring(4) + "<br/>");
document.write(str.substring(16) + "<br/>");
</script>
Output
www
includehelp
com
www.includehelp.com
includehelp.com
com
JavaScript String Object Methods »