Borislav Hadzhiev
Reading timeยท4 min
Photo from Unsplash
Use the String.startsWith()
method to check if a string starts with a
substring, e.g. if (str.startsWith(substring)) {}
.
The startsWith()
method will return true
if the string starts with the
substring and false
otherwise.
const str = 'bobbyhadz.com'; if (str.startsWith('bobby')) { // ๐๏ธ this runs console.log('โ string starts with substring'); } else { console.log('โ๏ธ string does NOT start with substring'); } console.log(str.startsWith('bobby')); // ๐๏ธ true console.log(str.startsWith('abc')); // ๐๏ธ false
We used the String.startsWith method to check if a string starts with a specific substring.
The method returns a boolean result:
true
if the string starts with the substringfalse
if it doesn'tstartsWith()
method performs a case-sensitive search. If you want to ignore the case, convert the string and the substring to lowercase.const str = 'BOBBYHADZ.COM'; const substr = 'bobby'; if (str.toLowerCase().startsWith(substr.toLowerCase())) { // ๐๏ธ this runs console.log('โ string starts with substring'); } else { console.log('โ๏ธ string does NOT start with substring'); }
Converting the string and the substring to lowercase enables us to perform a case-insensitive test for whether the string starts with the substring.
Alternatively, you can use the String.indexOf()
method.
Use the String.indexOf()
method to check if a string starts with a
substring, e.g. if (str.indexOf(substring) === 0) {}
.
If the indexOf()
method returns 0
, then the string starts with the
substring, otherwise, it doesn't.
const str = 'bobbyhadz.com'; const substr = 'bobby'; if (str.indexOf(substr) === 0) { // ๐๏ธ this runs console.log('โ string starts with substring'); } else { console.log('โ๏ธ string does NOT start with substring'); }
The String.indexOf method returns the index of the first occurrence of a substring in a string.
If the substring is not contained in the string, the method returns -1
.
If the method returns 0
, then the string starts with the substring.
You can also use the RegExp.test()
method to check if a string starts with a
substring.
Use the RegExp.test()
method to check if a string starts with a substring.
The method will return true
if the string starts with the supplied substring
and false
otherwise.
if (/^abc/.test('abc123')) { // ๐๏ธ this runs console.log('โ string starts with abc'); } else { console.log('โ๏ธ string does NOT start with abc'); }
We used the RegExp.test method to check if a string starts with a specific substring.
The method returns true
if the regular expression is matched in the string and
false
otherwise.
The forward slashes / /
mark the start and end of the regular expression.
The caret ^
matches the beginning of the input.
In this example, we check if the string abc123
starts with the substring
abc
.
If you want to make the regular expression case insensitive, add the i
flag.
// ๐๏ธ with `i` flag if (/^abc/i.test('ABC123')) { // ๐๏ธ this runs console.log('โ string starts with abc'); } else { console.log('โ๏ธ string does NOT start with abc'); }
The i
flag allows us to perform case insensitive search in the string.
If you ever need help reading a regular expression, check out this regular expression cheatsheet by MDN.
It contains a table with the name and the meaning of each special character with examples.
Use the String.startswith()
method with the logical OR (||) operator to
check if a string starts with one of multiple values.
If the string starts with one of the specified values, true
is returned,
otherwise, false
is returned.
const str = 'one two three'; if (str.startsWith('one') || str.startsWith('two')) { // ๐๏ธ this runs console.log('โ string starts with one of the values'); } else { console.log('โ๏ธ string does NOT start with either value'); }
We used the String.startsWith method to check if a string starts with a specific substring.
startsWith()
method returns true
if the string starts with the substring and false
otherwise.The logical OR (||) operator allows us to chain multiple conditions.
If either call to the startsWith()
method returns true
, our if
block will
run.
You can use this approach to chain as many conditions as necessary.
const str = 'one two three'; if (str.startsWith('one') || str.startsWith('two') || str.startsWith('three')) { // ๐๏ธ this runs console.log('โ string starts with one of the values'); } else { console.log('โ๏ธ string does NOT start with either value'); }
Alternatively, you can use the Array.some()
method.
some()
#To check if a string starts with one of multiple values:
Array.some()
method to iterate over the array.function startsWithOne(string, values) { return values.some(element => { return string.startsWith(element); }); } const str = 'one two three'; // ๐๏ธ true console.log(startsWithOne(str, ['one', 'two', 'three'])); // ๐๏ธ false console.log(startsWithOne(str, ['abc', 'xyz']));
We wrapped the multiple values we want to test for in an array.
The function we passed to the Array.some method gets called with each element in the array.
On each iteration, we check if the string starts with the current value and return the result.
The some()
method short-circuits and returns true
if the callback function
returns a truthy value at least once, otherwise, false
is returned.
Alternatively, you can use the String.indexOf()
method.
To check if a string starts with one of multiple values:
String.indexOf()
method with the logical OR
(||) operator.String.indexOf()
method returns 0
for any of the calls, the string
starts with one of the values.const str = 'one two three'; if (str.indexOf('one') === 0 || str.indexOf('two') === 0) { console.log('โ string starts with either value'); } else { console.log('โ๏ธ string does NOT start with either value'); }
The String.indexOf method returns the index of the first occurrence of a substring in a string.
If the substring is not contained in the string, the method returns -1
.
If the substring is found in the string at index 0
, then the string starts
with the substring.
If either call to the indexOf
method returns 0
, our if
block will run.