.startsWith()
Anonymous contributor
Anonymous contributor3071 total contributions
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 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:
All contributors
- Anonymous contributorAnonymous contributor3071 total contributions
- EugeneGoh_51 total contributions
- Armstrong20355 total contributions
- christian.dinh2476 total contributions
- Anonymous contributor
- EugeneGoh_
- Armstrong2035
- christian.dinh
Looking to contribute?
- 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.