Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript String startsWith() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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




function func() {
    let str = 'Geeks for Geeks';
    let value = str.startsWith('Gee');
    console.log(value);
}
func();


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




// JavaScript to illustrate startsWith() method
 
function func() {
 
    // Original string
    let str = 'It is a great day.';
 
    // Checking the condition
    let value = str.startsWith('It');
    console.log(value);
}
func();


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




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


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




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


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
Last Updated : 22 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials