Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Web Developement

JavaScript String startsWith() Method (with Examples)

Komal Bhatia by Komal Bhatia
December 23, 2023
Reading Time: 5 mins read
JavaScript String startsWith Method
Follow us on Google News   Subscribe to our newsletter

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.

ShareTweetShareSendSend
Komal Bhatia

Komal Bhatia

I am a dedicated Computer Science student with a strong track record of academic excellence. I am a highly motivated individual with a passion for providing creative and innovative solutions to complex problems. I possess skills in the domain of C++ and web development and am always eager to contribute to the field of Software Development.

RelatedPosts

Javascript Animation Libraries

Top 10 JavaScript Animation Libraries in 2025

February 19, 2025
JavaScript Interview Questions

Top 40 JavaScript Interview Questions and Answers in 2024

April 1, 2025
Best React UI Component Libraries

10 Must-Know React UI Component Libraries for Web Devs 2024

May 7, 2024
Currying in JavaScript

Currying in JavaScript Explained (with Examples)

March 15, 2024
Javascript Format Currency

Javascript Program for Format Currency

April 8, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.