JavaScriptStringsBeginner

String.startsWith()

Checks if a string starts with a substring (true/false).

Review the syntaxStudy the examplesOpen the coding app
string.startsWith(searchString, position?)

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Checks if a string starts with a substring (true/false).

The startsWith() method determines whether a string begins with the characters of a specified search string, returning true or false as appropriate. This method is case-sensitive and does not support regular expressions as the search pattern. Performance-wise, it is highly efficient for prefix validation compared to methods like indexOf() == 0 or substring comparisons, as it stops execution as soon as a mismatch is found. Edge cases include passing an empty string as the search pattern, which always returns true, or providing a negative position, which is treated as 0. If the position parameter is greater than or equal to the length of the string, the method returns false (unless the search string is empty). This method was introduced in ES6, so Internet Explorer is not supported.

Quick reference

Syntax

string.startsWith(searchString, position?)

Inputs

Parameters

searchStringstring · Prefix to check for.
position (optional)number · Index to start checking from (default 0).

See it in practice

Examples

1

Basic Prefix Validation

const protocol = 'https://api.example.com';
console.log(protocol.startsWith('https'));
Output:
true

Determines if a URL string begins with the secure protocol prefix.

2

Searching from a Custom Position

const phrase = 'The quick brown fox';
console.log(phrase.startsWith('quick', 4));
Output:
true

Using the optional position parameter to check if a word exists at a specific index.

3

Case-Insensitive Prefix Check

const filename = 'README.md';
const search = 'readme';
const result = filename.toLowerCase().startsWith(search.toLowerCase());
console.log(result);
Output:
true

Normalization using toLowerCase() is required because startsWith() is natively case-sensitive.

Debug faster

Common Errors

1

TypeError

Cause: Passing a Regular Expression as the searchString argument.

Fix: Use a plain string for startsWith(), or use RegExp.test() with the '^' anchor if regex functionality is required.

'hello'.startsWith(/he/);
2

TypeError

Cause: Calling startsWith on a null or undefined reference.

Fix: Ensure the variable is a string or use optional chaining or a null check before calling the method.

var str = null;
str.startsWith('a');

Runtime support

Compatibility

Node.js, BrowserAll modern browsers, IE not supportedES6 (2015)

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Checks if a string starts with a substring (true/false).

searchString: Prefix to check for. position: Index to start checking from (default 0).

TypeError: Use a plain string for startsWith(), or use RegExp.test() with the '^' anchor if regex functionality is required. TypeError: Ensure the variable is a string or use optional chaining or a null check before calling the method.