DEV Community

Cover image for JavaScript startsWith and multiple conditions
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

18 2

JavaScript startsWith and multiple conditions

You might have heard of the JavaScript startsWith method. It can check if a particular string starts with another string.

To give you a demonstration it would work something like this:

const string = 'Hi, and welcome from JavaScript';
console.log(string.startsWith('Hi'));
// true
console.log(string.startsWith('Hello'));
// false
Enter fullscreen mode Exit fullscreen mode

Checking for multiple conditions with startsWith

But what if we want to check if a string starts with a multiplication of strings?

So let's say Hi and 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 string = 'Hi, and welcome from JavaScript';
const result = string.startsWith('Hi') || string.startsWith('Hello');
console.log(result);
// true
Enter fullscreen mode Exit fullscreen mode

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));
console.log(result);
// true
Enter fullscreen mode Exit fullscreen mode

Feel free to try these out in the following CodePen.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Jetbrains image

Is Your CI/CD Server a Prime Target for Attack?

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. It makes sense—CI/CD servers have access to source code, a highly valuable asset. Is yours secure? Check out nine practical tips to protect your CI/CD.

Learn more

Oldest comments (4)

Collapse
 
lexlohr profile image
Alex Lohr

You can also directly use a bound method, i.e.

const result2 = ["Hi", "Hello"].some(string.startsWith.bind(string));
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dailydevtips1 profile image
Chris Bongers

Oh nice one! 🎉

Collapse
 
xahin289 profile image
Jannatun Nayeem

Really a helpful post. Tottally understand the logic, Thank you very much.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Thanks Jannatun!
Glad it's clear 👏

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay