/

HTML / CSS / JavaScript Tutorial

JavaScript String method: startsWith()

[this page | pdf | back links]

The startsWith() method (when applied to a JavaScript string) returns true if the string starts with a specified string, otherwise returns false.

 

It has the following syntax with the following parameters:

 

string.startsWith(searchvalue,startposition)

 

Parameter

Required / Optional

Description

searchvalue

Required

Value to be searched for

startposition

Optional

(default is 0). Position in underlying string from which to search from

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var y = "abcde";
var y2 = "cd";
document.getElementById("Example").innerHTML =
  'var y = "' + y + '";<br>' +
  'var x = y.startsWith("' + y2 + '",2);';
document.getElementById("Result").innerHTML =
  y.startsWith(y2,2);
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodStringStartsWith() {
  var z = "abc"; return !!z.startsWith;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript String Variables


Desktop view | Switch to Mobile