JavaScript | String startsWith()

str.startsWith() function is used to check whether the given string start with the characters of the specified string or not.

Syntax:
str.startsWith(searchString , position)

Arguments The first argument to this function is a string of characters searchString which is to be searched at the end of the given string. The second argument to the function is position which determines the position in the given string from where the searchString is to be searched.

Return value This function returns the Boolean value true if the searchString is found else it returns the Boolean value false

Examples for the above function are provided below:

Example 1:



var str = 'It is a great day.';
var value = str.startsWith('It'); 
print(value);

Output:

true

In the above example the function startsWith() checks whether the string str starts with It or not. Since the string starts with It therefore this function returns true.

Example 2:

var str = 'It is a great day.';
var value = str.startsWith('great');
print(value);

Output:

false

In this example the function startsWith() checks whether the string str starts with great or not. Since great appears in the middle and not in the beginning of the string therefore this function returns false.

Example 3:

var str = 'It is a great day.'
var value = str.startsWith('great',8);
print(value);

Output:

true

In this example the function startsWith() checks whether the string str starts with great or not at the specified index 8. Since great appears at the given index in the string therefore this function returns true.

Codes for the above function are provided below:

Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate startsWith() function
  
function func() {
       
    // Original string
    var str = 'It is a great day.';
      
    // Checking the condition
    var value = str.startsWith('It'); 
    document.write(value);
}
func();
</script>

chevron_right


Output:

true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate startsWith() function
<script>
function func() {
      
    // Original string
    var str = 'It is a great day.';
    var value = str.startsWith('great');
    document.write(value);
}
func();
</script>

chevron_right


Output:

false

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate startsWith() function
  
function func() {
     
    // Original string
    var str = 'It is a great day.';
    var value = str.startsWith('great');
    document.write(value);
}
func();
</script>

chevron_right


Output:

true


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.