How do I check if a string starts with another string in JavaScript?

To check if a string starts with another string in JavaScript, you can use the string.startsWith(searchString, index) method. The startsWith() method determines if the start of this string matches the specified string or character. The first parameter specifies the search string. The second parameter is optional and specifies the index in the given string from which to search. The default parameter value is zero. The startsWith() method returns a boolean value - "true" if the given string starts with the specified string, "false" otherwise. You can also determine if the end of a given string matches the specified string or character using the endsWith() method. The methods are case-sensitive. In this JavaScript example, we check if a string starts with a given string using the startsWith() method. Click Execute to run the JavaScript String Startswith online and see the result.
Loading...