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(s…


This content originally appeared on DEV Community and was authored by Chris Bongers

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

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

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

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


This content originally appeared on DEV Community and was authored by Chris Bongers


Print Share Comment Cite Upload Translate Updates
APA

Chris Bongers | Sciencx (2021-12-01T09:19:14+00:00) JavaScript startsWith and multiple conditions. Retrieved from https://www.scien.cx/2021/12/01/javascript-startswith-and-multiple-conditions/

MLA
" » JavaScript startsWith and multiple conditions." Chris Bongers | Sciencx - Wednesday December 1, 2021, https://www.scien.cx/2021/12/01/javascript-startswith-and-multiple-conditions/
HARVARD
Chris Bongers | Sciencx Wednesday December 1, 2021 » JavaScript startsWith and multiple conditions., viewed ,<https://www.scien.cx/2021/12/01/javascript-startswith-and-multiple-conditions/>
VANCOUVER
Chris Bongers | Sciencx - » JavaScript startsWith and multiple conditions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/01/javascript-startswith-and-multiple-conditions/
CHICAGO
" » JavaScript startsWith and multiple conditions." Chris Bongers | Sciencx - Accessed . https://www.scien.cx/2021/12/01/javascript-startswith-and-multiple-conditions/
IEEE
" » JavaScript startsWith and multiple conditions." Chris Bongers | Sciencx [Online]. Available: https://www.scien.cx/2021/12/01/javascript-startswith-and-multiple-conditions/. [Accessed: ]
rf:citation
» JavaScript startsWith and multiple conditions | Chris Bongers | Sciencx | https://www.scien.cx/2021/12/01/javascript-startswith-and-multiple-conditions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.