JavaScript String.prototype.startsWith() Method
The startsWith() method checks whether a string begins with given text, returning true or false.
Description
The startsWith() method determines whether a string begins with the characters of another string. It's case-sensitive and is clearer than indexOf(x) === 0.
The optional second argument sets the position to start checking from, which lets you test the text after a known prefix without slicing. An empty search string always returns true, and passing a regular expression throws a TypeError.
To check for several possible prefixes, use some() on an array of them, or a regular expression with the ^ anchor, such as /^https?:\/\// to accept both http:// and https://.
Syntax
string.startsWith(searchString)
string.startsWith(searchString, position)Parameters
- searchStringThe text to look for at the start.
- positionOptional. The index to treat as the start.
Return Value
true if the string starts with the search text, otherwise false.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.startsWith() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





