Skip to content
Related Articles

Related Articles

JavaScript String startsWith() Method
  • Last Updated : 01 Sep, 2020

Below is the example of the String startsWith() Method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
    function func() {
        var str = 'Geeks for Geeks';
        var value = str.startsWith('Gee'); 
        document.write(value);
    }
    func();
    </script>

    chevron_right

    
    

  • Output:
    true
    

The str.startsWith() method is used to check whether the given string starts with the characters of the specified string or not.

Syntax:

str.startsWith( searchString , position )

Parameters: This method accepts two parameters as mentioned above and described below:

  • searchString: It is required parameter. It stores the string which needs to search.
  • start: It determines the position in the given string from where the searchString is to be searched. The default value is zero.

Return value This method returns the Boolean value true if the searchString is found else returns false.



Examples for the above method 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 method startsWith() checks whether the string str starts with It or not. Since the string starts with It therefore it returns true.

Example 2:

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

Output:

false

In this example, the method 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 it 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 method
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 it returns true.

Below programs illustrate the startsWith() method in JavaScript:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate startsWith() method
  
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

<script>
  
// JavaScript to illustrate 
// startsWith() method
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() method
function func() {
       
    // Original string
    var str = 'It is a great day.';
    var value = str.startsWith('great', 8);
    document.write(value);
}
  
// Function call
func();
  
</script>

chevron_right


Output:

true

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :