JavaScript .startsWith()

Armstrong2035's avatar
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.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

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:

Code
Output

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours