Javatpoint Logo
Javatpoint Logo

JavaScript String startsWith

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.

Syntax

In the above syntax:

  • str: The specified string
  • searchString: The characters are specified which are to be searched in the str.
  • position: An optional parameter that holds the position in the string from where to begin the searching. By default, the value is 0.

Return Value

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:

Example 1:

Below is an example code:

Test it Now

In the above code example, we have assigned a string to a const variable 'test':

  • log(test.startsWith('Work'));

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.

  • log(test.startsWith('Worship',8));

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.

  • log(test.startsWith('work'));

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.

Output:

JavaScript String startsWith

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.

Example 2:

Below is a code example:

Test it Now

Output:

JavaScript String startsWith

In the above code,

  • We have created a function test () within which we have initialized a string for variable str.
  • Next, we have specified some character string that is to be searched in the specified string.
  • We have not set the position value, and so it searches from the 0th position in the string.
  • As it finds that the specified characters match with the string characters specified in the string. So, it returns the output as true because the match is found. Thus, the value of the variable x is assigned as true. Hence, on invoking the function, the output is returned as true.





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA