.startsWith()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Jun 20, 2021Updated Aug 8, 2023
Contribute to Docs

The .startsWith() JavaScript string method checks whether a string begins with the specified characters. It returns true if the string begins with the specified characters, otherwise, it returns false.

Syntax

myString.startsWith(substring, position);
  • The substring is the specified characters that will be checked for in the string. It is case-sensitive.
  • The position is optional. It is the index used to start searching for substring. It defaults to 0.

Example 1

Checking if a string starts with a particular character:

console.log('Do you prefer coffee or tea?'.startsWith('D'));
// Output: true
// In the original string, "D" is capitalized. But in the substring parameter, 'd' is in lowercase:
console.log('Do you prefer coffee or tea?'.startsWith('d'));
// Output: false

Codebyte Example

The following example is runnable and checks if a string, starting at a specified index, starts with a particular substring:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn JavaScript on Codecademy