startsWith method. It can check if a particular string starts with another string.
To give you a demonstration it would work something like this:
Checking for multiple conditions with startsWith permalink
But what if we want to check if a string starts with a multiplication of strings?
So let's say
Hello would both be fine.
We could use a conditional statement. However, this might get very unorganized if we decide to allow more strings at a later stage.
However, it would look like this:
const result = string.startsWith('Hi') || string.startsWith('Hello');
Another way is to use the same method on a predefined array. I quite like the simplicity and naming of this method as it really states what's happening.
This is what it looks like:
const result = ['Hi', 'Hello'].some(word => string.startsWith(word));
Feel free to try these out in the following CodePen.