startsWith() and multiple strings

Let's assume that you want to check if a string starts with multiple other strings.

For example, let's consider the following string:

javascript
let string = 'Hello, my name is Elon';

Let's assume that you want to check if the string starts with 'lo' or 'He'.

Conditional

You can do it using a conditional:

javascript
let res = string.startsWith('lo') || string.startsWith('He');

console.log(res); // true

Array

You can do it using an array:

javascript
let res = ['lo', 'He'].some(word => string.startsWith(word));

console.log(res); // true

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.