Home » JavaScript Tutorial

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.