Sunday, June 14, 2026
HomeLanguagesJavascriptHow to check if a string “StartsWith” another string in JavaScript ?

How to check if a string “StartsWith” another string in JavaScript ?

In JavaScript, the startsWith()’ method is a built-in method that allows one to check whether a string starts with a specified substring. 

Syntax: It takes a single argument, which is the substring we want to check whether the string starts with.

string.startsWith(searchString[, position])

Approach:

The ‘searchString’ parameter is the substring that you want to search for at the beginning of the string. The ‘position’ parameter is an optional parameter that specifies the position in the string to start the search. If not specified, the search starts from the beginning of the string (position 0).

Example 1: This example shows the use of the above-explained approach.

Javascript




const str = 'Hello Geeks!';
 
console.log(str.startsWith('Hello')); // true
console.log(str.startsWith('Geeks', 6)); // true
console.log(str.startsWith('Geeks', 7)); // false


Output:

true
true
false

Example 2: This example shows the use of the above-explained approach.

Javascript




let x ="Hello World!"
function myfunc() {
    if (x.startsWith('Hello')) {
        result = true;
    } else {
        result = false;
    }
console.log(result);
}
myfunc();


Output:

true
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS