The startsWith () is a method of string that determines if the string starts with the specified characters of a string. Then results either true or false, whichever is appropriate as an output.
Here, in this section, we will discuss the startsWith () method with some example implementation to know how the method is being used.
JavaString String startsWith () Method
The method returns true if it finds that the input string begins with the specified characters of a string else returns false as an output. Also, the startsWith () method is case-sensitive.
In the above syntax:
The string function returns true if the specified characters are found in the string; otherwise it returns false.
Example of String startsWith () Method
Let's see some example implementation to understand the concept better:
Below is an example code:Test it Now
In the above code example, we have assigned a string to a const variable 'test':
Now, when we console log the string with searchString = Work, and no position is specified, so the searchString will search for its value from the 0th position, and as beginning the search, the characters are found, and so it returns true as an output.
Now, when we console log the string with searchString = Worship and also specified the position value as 8. So, the searchString will begin searching from the 8th position from the beginning, and at the 8th position, it finds the matched characters; thus, it returns true as the output.
Now, when we console log the string with searchString = work with no specified position, the searches begin from 0th position, and as the first character does not match so, it returns false. It outputs false because the string startsWith () method is case-sensitive.
Note: The default position value 0 starts from the first character of the string. Thus, if the 0th position in the string is a single space, the output will be 0 with the above searchString values.
Below is a code example:Test it Now
In the above code,
Next TopicJS First Class Function