String.prototype.startsWith
Determines whether a string begins with the characters of a specified string
Syntax
string.startsWith(searchString, position?)Parameters
| Parameter | Type | Description |
|---|---|---|
| searchString | string | The characters to be searched for at the start |
| position | number | Position at which to begin searching |
Return Value
true if the string begins with the search string, false otherwise
Examples
const str = 'Hello World';
console.log(str.startsWith('Hello')); // true
console.log(str.startsWith('World')); // falseconst url = 'https://example.com';
if (url.startsWith('https://')) {
console.log('Secure connection');
}const filename = 'readme.md';
console.log(filename.startsWith('read')); // trueUnderstanding String.prototype.startsWith
The String.prototype.startsWith method in JavaScript determines whether a string begins with the characters of a specified string. It belongs to the String object and is one of the most widely used methods for working with string values in modern JavaScript and TypeScript applications.
The method signature is string.startsWith(searchString, position?). It accepts 2 parameters: searchString, position. When called, it returns true if the string begins with the search string, false otherwise. Understanding when and how to use startsWith() helps you write more expressive, readable code.
Common use cases for String.prototype.startsWith include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like string-endswith, string-includes, string-indexof, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for String.prototype.startsWith is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
String.prototype.endsWithDetermines whether a string ends with the characters of a specified string
String.prototype.includesDetermines whether one string may be found within another string, returning true or false
String.prototype.indexOfReturns the index of the first occurrence of a specified value in a string, or -1 if not found
More String Methods
Other methods in the String object
Related Tools
More String Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.