API · JavaScript

JavaScript String.startsWith() Method

JavaScript string startsWith() is a method that returns a boolean value after determining whether the given string starts with a specified string value.

On this page

JavaScript String.prototype.startsWith() returns a Boolean indicating whether a string begins with a given search value at a chosen position. With no position argument, it checks from the first character. It does not search for the value elsewhere in the string: choose a position when the expected prefix begins later.

What startsWith() checks

The method compares the characters in searchValue with the characters beginning at position. It returns true when they match in sequence and false when they do not. The comparison is case-sensitive, so uppercase and lowercase letters are different. Punctuation, spaces, and other characters are part of the comparison just like letters.

The original string is not changed. The method reports a match at one location; it does not return the matching text or the location of a match later in the string. Use its Boolean result directly in a condition when the next action depends on whether a prefix is present.

Syntax and parameters

Call the method on the string being checked: string.startsWith(searchValue) or string.startsWith(searchValue, position). searchValue is the value to compare at the start position. Values that JavaScript can convert to strings are converted before comparison, but a Symbol value cannot be converted by this method and causes a TypeError. Separately, a RegExp object recognized as a regular expression by Symbol.match also causes a TypeError; if its Symbol.match property is false, it is not rejected by that check and can instead be converted to a string. startsWith() does not perform regular-expression matching.

position is optional and is measured in string indices, starting at zero. These indices count UTF-16 code units, so some characters such as emoji occupy two positions. If omitted, it defaults to 0. A negative position is treated as the beginning of the string; a position beyond its length is treated as its end. Consequently, an empty search value matches at any position after this adjustment, while a nonempty value cannot match beyond the last place where its characters fit.

Examples: default and custom positions

This example checks a prefix at the default position, then checks another prefix at index 11. JavaScript string indices are zero-based, so index 11 is the c in Typing the code.. The final call checks a different capitalization at the beginning. The output is true, true, and false, in that order.

Run this example:

const message = "Typing the code.";
console.log(message.startsWith("Typing"));
console.log(message.startsWith("code", 11));
console.log(message.startsWith("typing"));
true
true
false

Matching rules and limitations

Each comparison is exact and case-sensitive. For example, "JavaScript".startsWith("Java") is true, but "JavaScript".startsWith("java") is false. Punctuation is significant too: "Welcome to 'coding'.".startsWith("'cod", 11) is true because the apostrophe is the first character at index 11. Leaving punctuation out or changing it changes the search value being compared.

The optional position lets you test a known offset, not scan for a value. To check whether text occurs anywhere, use a search method designed for that task. Also remember that startsWith() checks a string prefix, not a regular-expression pattern; use a regular-expression operation when the match depends on a pattern rather than literal characters.