.startsWith()
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 forsubstring
. 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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours