Home » JavaScript

JavaScript String startsWith() Method with Example

JavaScript String startsWith() Method: Here, we are going to learn about the startsWith() method in JavaScript with Example.
Submitted by IncludeHelp, on February 06, 2019

String startsWith() Method

startsWith() method is a string method in JavaScript, it is used to check whether a string starts with a specified substring or not.

It returns true – if string starts with a specified substring, and it returns false – if string does not start with the specified substring.

Syntax:

    String.startsWith(substring);

Examples:

    Input:
    str = "IncludeHelp is made for students.";
    substring = "IncludeHelp"
    //function call
    str.startsWith(substring);
    Output:
    True

    Input:
    str = "IncludeHelp is made for students.";
    substring = "Hello"
    //function call
    str.startsWith(substring);
    Output:
    False

Code:

<html>
<head>
<title>JavaScipt Example</title>
</head>

<body>
<script>
	var str = "IncludeHelp is made for students.";
	
	var substring = "IncludeHelp";	
	if(str.startsWith(substring)){
		document.write(str + " starts with " + substring + "<br>");
	}
	else{
		document.write(str + " does not start with " + substring + "<br>");
	}

	substring = "Hello";
	if(str.startsWith(substring)){
		document.write(str + " starts with " + substring + "<br>");
	}
	else{
		document.write(str + " does not start with " + substring + "<br>");
	}	
</script>
</body>
</html>

Output

IncludeHelp is made for students. starts with IncludeHelp
IncludeHelp is made for students. does not start with Hello





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.