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);
Sample Input/Output
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
Example
<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
JavaScript String Object Methods »