Tuesday, September 24, 2024
Google search engine
HomeLanguagesJavascriptJavaScript String startsWith() Method

JavaScript String startsWith() Method

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




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

Example 5 : In this example, we are checking if the URL variable starts with “https://”. It returns true. It also checks if it starts with “http://”, which returns false, and also we check if the filename variable starts with “.js”. It returns false. It also checks if it starts with “script.”, which returns true.

Javascript




// Checking if a URL starts with "https://"
 
console.log(url.startsWith('https://'));
console.log(url.startsWith('http://'));
 
//Checking if a filename starts with a specific extension
let filename = 'script.js';
 
console.log(filename.startsWith('.js'));
console.log(filename.startsWith('script.'));


Output

true
false
false
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

RELATED ARTICLES

Most Popular

Recent Comments