To check if a string starts with a specific prefix in JavaScript, you can use the 'startsWith' method of the String object.
In this example,
function startsWithPrefix(str, prefix) {
return str.startsWith(prefix);
}
let str = 'Hello World';
let prefix = 'Hello';
let startsWith = startsWithPrefix(str, prefix);
console.log(`String starts with prefix '${prefix}': ${startsWith}`);
String starts with prefix 'Hello': true
In this tutorial, we learned How to Check if String starts with Specific Prefix in JavaScript language with well detailed examples.