JavaScript Program to Check if a String Starts With Another String Using lastIndexOf() Method

What we are going to look at today is JavaScript Program to Check if a String Starts With Another String Using lastIndexOf() Method We are going to be very clear about this topic. We hope that all the skepticism on this topic will be resolved through this article.

JavaScript Program to Check if a String Starts With Another String Using lastIndexOf() Method

JavaScript Program to Check if a String Starts With Another String Using lastIndexOf() Method

// program to check if a string starts with another string

const string = 'hello world';

const toCheckString = 'he';

let result = string.lastIndexOf(toCheckString, 0) === 0;
if(result) {
    console.warn('The string starts with "he".');
}
else {
    console.warn(`The string does not starts with "he".`);
}

Read Also: JavaScript Program to Check if a String Starts With Another String Using Regular Expression

Final Words

We hope you find the JavaScript Program to Check if a String Starts With Another String Using lastIndexOf() Method useful. And please let us know if you have any doubts about this article.

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment