String.prototype.startsWith()

startsWith(searchString[, position])
Returns: boolean · Added in vES6 · Updated March 16, 2026 · String Methods
string startsWith es6

The startsWith() method determines whether a string begins with the characters of a specified search string. It returns true if the string starts with the search string, and false otherwise. This method is case-sensitive and was introduced in ES6 as a cleaner alternative to manual prefix checking using indexOf() or substring comparisons.

One important detail: the method treats all non-regex values as strings through type coercion, so passing undefined will search for the literal string “undefined” rather than behaving as you might expect.

Syntax

string.startsWith(searchString)
string.startsWith(searchString, position)

Parameters

ParameterTypeDefaultDescription
searchStringstringThe characters to search for at the start of the string. Cannot be a regex. All values are coerced to strings.
positionnumber0The start position at which searchString is expected to be found. Defaults to 0, meaning the beginning of the string.

Parameter Details

  • If searchString is an empty string, the method always returns true.
  • Passing undefined as searchString searches for the string “undefined” — this is rarely the intended behavior.
  • The position parameter specifies where to start the search, but it’s the index in the original string where the search begins, not an offset into the search string.
  • A TypeError is thrown if searchString is a regular expression.

Examples

Basic Prefix Checking

const str = "Saturday night plans";

console.log(str.startsWith("Sat"));    // true
console.log(str.startsWith("sat"));    // false (case-sensitive)
console.log(str.startsWith("night"));  // false

The most common use case is validating whether a string begins with a specific prefix. Here, “Sat” matches but the lowercase variant doesn’t because the method is case-sensitive.

Using the Position Parameter

const str = "To be, or not to be, that is the question.";

console.log(str.startsWith("To be"));        // true
console.log(str.startsWith("To be", 0));     // true (explicit start)
console.log(str.startsWith("not to be"));    // false
console.log(str.startsWith("not to be", 10)); // true

console.log(str.startsWith("To be", 1));      // false

The position parameter lets you check for a prefix starting from a specific index. When you pass 10, it looks at the substring starting at position 10 (“not to be, that is the question.”), which does begin with “not to be”. This is useful when parsing structured strings or validating segments.

Checking Protocol and File Types

const url = "https://example.com/api/users";

console.log(url.startsWith("https://"));  // true
console.log(url.startsWith("http://"));   // false

const filename = "report-2024.pdf";
console.log(filename.startsWith("report"));  // true
console.log(filename.endsWith(".pdf"));     // true

A practical application is validating URL protocols or file extensions. Combining startsWith() with endsWith() gives you a simple way to check both prefix and suffix.

Working with Dynamic Content

function formatMessage(type, text) {
  const prefix = `[${type.toUpperCase()}]`;
  if (text.startsWith(prefix)) {
    return text; // Already formatted
  }
  return `${prefix} ${text}`;
}

console.log(formatMessage("info", "System ready"));    // "[INFO] System ready"
console.log(formatMessage("info", "[INFO] Duplicate")); // "[INFO] Duplicate"

When building logging or messaging systems, you often need to check if content is already formatted before applying formatting. This pattern prevents double-formatting.

Common Patterns

Validating command input:

const commands = ["help", "status", "exit"];

function isValidCommand(input) {
  return commands.some(cmd => input.startsWith(cmd));
}

console.log(isValidCommand("help me"));     // true
console.log(isValidCommand("status"));       // true
console.log(isValidCommand("restart now")); // false

Safe string concatenation:

function addPrefixIfNeeded(str, prefix) {
  return str.startsWith(prefix) ? str : prefix + str;
}

console.log(addPrefixIfNeeded("users", "role:")); // "role:users"
console.log(addPrefixIfNeeded("role:admins", "role:")); // "role:admins"

Filtering arrays by prefix:

const items = ["apple", "apricot", "banana", "blueberry"];

const filtered = items.filter(item => item.startsWith("a"));
console.log(filtered); // ["apple", "apricot"]

Checking string prefixes in conditional logic:

const userInput = "delete-all";

if (userInput.startsWith("delete-")) {
  console.log("⚠️  This is a destructive command");
} else if (userInput.startsWith("get-")) {
  console.log("📤 This is a read operation");
}

Next Steps

Now that you understand how to check if a string begins with a specific prefix, explore other string methods for different types of matching. If you need to check the end of a string instead, endsWith() provides the opposite functionality. For finding substrings anywhere in the string, includes() is the method you want.

For more complex pattern matching beyond simple prefix checks, consider using regular expressions with the match() or test() methods. However, for straightforward prefix validation, startsWith() offers better readability and intentionality in your code.

See Also