Strings represent a sequence of multiple characters and it is crucial to know how to manipulate and play with strings in any programming language. One of the ways to manipulate strings is by using startsWith in JavaScript.
What is the startsWith() Function in JavaScript?
Before moving forward, let’s revise our understanding of Strings. Strings in JavaScript represent a sequence of multiple characters. We can declare a string in JavaScript by making a string literal using double quotes. The syntax for the same is given below:
//declaring a string literal let a = "FavTutor" //printing the string console.log(a);
Output:
FavTutor
So, where does startsWith() fit in?
The startsWith() is a built-in JavaScript method to determine or check whether a string starts with a specified set of characters. The method returns a bool value indicating the presence of the characters at the beginning of the string.
The syntax of the method is given below:
stringName.startsWith(stringCheck, position);
where,
- stringName: The original string on which we have to perform the check
- stringCheck: The substring to be checked for at the beginning of the original string.
- position(optional parameter): The position within the original string at which to begin the search. By default, position would be index 0, i.e. beginning of the string.
It returns a boolean value: true if the string starts with the specified characters, and false otherwise. The method first compares the characters of the given string with the given sequence of characters. If the characters match from the beginning of the string, the function returns true. However, if doesn’t match it returns false.
Let us see the use of startsWith with an example:
//declaring the original string let ogString="FavTutor"; //declaring the check string let subString="Fav"; //checking if substring is part of the original string let checkBool=ogString.startsWith(subString); //printing the return value console.log(checkBool);
Output:
True
In this example, we have taken 2 strings, one original string(ogString), and a check string(subString). We check the presence of the subString in the ogString. We have used no position parameter in the function, so by default, we check the presence of the subString at the starting index, i.e. 0 of the ogString.
Positional Parameter
Let us see another example, of using the positional parameter with the startsWith function.
//declaring the original string let ogString="FavTutor"; //declaring the check string let subString="Fav"; //checking if substring is part of the original string let checkBool=ogString.startsWith(subString,2); //printing the return value console.log(checkBool);
Output:
false
In this example, we have provided the position parameter as 2. This means that we will verify the presence of the characters of the subString starting from the second index of the original string(ogString) that is from the letter ‘v’. Hence we have got the result as false. This is the desired result.
Let us see a similar example:
//declaring the original string let ogString="FavTutor"; //declaring the check string let subString="Tutor"; //checking if substring is part of original string let checkBool=ogString.startsWith(subString,3); //printing the return value console.log(checkBool);
Output:
true
Now, in this example, we are using the positional parameter as 3. We are checking the subString “Tutor” to be present in the string “FavTutor ” at the 3rd index. As the indices start from 0 the string “Tutor” occurs at the 3rd index in the ogString. Hence, we have got the desired result.
Case Sensitivity
It is also important to note the case sensitivity of the startsWith function that the uppercase and lowercase letters are treated differently. Let us see an example of the same:
//declaring the original string let ogString="FavTutor"; //declaring the check string let subString="fav"; //checking if substring is part of original string let checkBool=ogString.startsWith(subString); //printing the return value console.log(checkBool);
Output:
false
We are checking the presence of “fav” in the string “FavTutor”. Although the starting three characters are the same in the string “FavTutor”, the letter ‘f’ is present in uppercase and hence doesn’t match with the letter ‘f’ present in lowercase in the string “fav”. Hence the function returns false. We have got the desired result.
Conclusion
In this article, we have studied about JavaScript method “startsWith” which helps in finding whether a specified string is present in another string at a specified position. Let’s now learn how to concatenate strings in JavaScript to fully understand it.