Home >>JavaScript String Methods >JavaScript String startsWith() Method

JavaScript String startsWith() Method

JavaScript String startsWith() Method

JavaScript startsWith() method is used to determine if a string begins with the characters of a given specified string. It is case sensitive. It returns true value if the string begin with the character and false value if not.

Syntax:
string.startsWith(searchvalue, start)

Parameter Values

Parameter Description
searchvalue This is a required parameter. It defines the string to search for.
Start This is an optional parameter. It defines at which position to start the search.

Browser Support

Method Chrome Edge Firefox Safari Opera
startsWith() 41 12.0 17 9 28
Here is an example of startsWith() method:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str = "Welcome to PHPTPOINT..";
  var n = str.startsWith("Welcome");
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

Example 2:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function myFunction1() {
  var str = "Welcome to PHPTPOINT...";
  var n = str.startsWith("PHP", 11);
  document.getElementById("demo1").innerHTML = n;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.