To check if a string starts with another string, you can use the startsWith() string method in JavaScript.
For example, let's say you have a string called Hello World! and wants to check if the string starts with the word Hello, you can pass this string as an argument to the startsWith() method like this,
// a string
const str = "Hello World!";
// string to check
const strToCheck = "Hello";
// use startsWith() string method to check
// if string starts with another string
str.startsWith(strToCheck); // true
- The method returns a boolean value
trueif it starts with the supplied string andfalseif not.
See the code live in JSBin