Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript String startsWith() Method

Improve Article
Save Article
Like Article
  • Last Updated : 04 Jan, 2023
Improve Article
Save Article
Like Article

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

Syntax: 

str.startsWith( searchString , position )

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

  • searchString: It is a 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.

Example 1: Below is the example of the String startsWith() Method

JavaScript




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

Output: 

true

Example 2: 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.

JavaScript




<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');
        console.log(value);
    }
    func();
</script>

Output: 

true

Example 3: In this example, the method startsWith() checks whether the string str starts with great or not. Since great appears in the middle and not at the beginning of the string therefore it returns false.

JavaScript




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

Output: 

false

Example 4: 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.

JavaScript




<script>
    // JavaScript to illustrate
    // startsWith() method
    function func() {
         
        // Original string
        var str = 'It is a great day.';
        var value = str.startsWith('great', 8);
        console.log(value);
    }
     
    // Function call
    func();
</script>

Output: 

true

We have a complete list of Javascript String Methods, to check those please go through the Javascript String Complete Reference article.

Supported Browser:

  • Chrome 41 and above
  • Edge 12 and above
  • Opera 28 and above
  • Firefox 17 and above
  • Safari 9 and above

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!