JavaScript String startsWith

Mehvish Ashiq Oct 12, 2023
  1. Use of String startsWith() Method in JavaScript
  2. Use indexOf() Function to Check if String Starts With Another String in JavaScript
  3. Use lastIndexOf() Function to Check if String Starts With Another String in JavaScript
  4. Use substring() Function to Check if String Starts With Another String in JavaScript
  5. Use Regex’s test() Method to Check if String Starts With Another String in JavaScript
  6. Use Custom Function With Loop to Check if String Starts With Another String in JavaScript
JavaScript String startsWith

There are some situations while coding when we want to ensure if the string starts with a particular string.

For example, I want to inspect if the given string begins with the Me string.

Today, we will learn about how to examine if the string starts with a particular string using startsWith(), indexOf(), lastIndexOf(), substring() functions, regex’s test() method and a custom function with loop.

Use of String startsWith() Method in JavaScript

ECMAScript 2015 (also called ES6) introduced the startsWith() method that checks if the string starts with a specified string or not. It outputs true if the match is found; otherwise, false.

The startsWith() method takes two arguments: the searchString and the second is position, which is optional and defaults to zero. If the position is specified, the function searches the string from that position.

In the following example, we are not specifying the position. So, it starts searching case-sensitively from index zero.

var name = 'Hi John, how are you doing?'
console.log(name.startsWith('Hi John'));

Output:

true

Example Code:

var name = 'Hi John, how are you doing?'
console.log(name.startsWith('Hi john'));

Output:

false

Example Code:

var name = 'Hi John, how are you doing?'
console.log(name.startsWith(
    'how', 9));  // it starts searching and matching from index 9

Output:

true

Use indexOf() Function to Check if String Starts With Another String in JavaScript

The indexOf() is used with arrays to locate the element and returns the first index where it is found, but we can also use the string in the following way.

If the index of the matched element is zero, it means the string starts with the specified string.

Example Code:

var name = 'Hi John, how are you doing?'
if (name.indexOf('Hi') === 0)
console.log(true);
else console.log(false);

Output:

true

It also searches case-sensitively. Have a look at the following example code.

var name = 'Hi John, how are you doing?'
if (name.indexOf('hi') === 0)
console.log(true);
else console.log(false);

Output:

false

What if we want indexOf() to locate the element from the specified position?

var name = 'Hi John, how are you doing?'
if (name.indexOf('how') === 9)
console.log(true);
else console.log(false);

Output:

true

Use lastIndexOf() Function to Check if String Starts With Another String in JavaScript

This function returns the last occurrence’s position/index of the particular value in a string.

It starts searching from the end and moves towards the beginning of the string. It returns -1 if the value is not there.

We can modify a bit and use the lastIndexOf() method in the following manner to see if the string begins with another string.

Example Code:

var name = 'Hi John, how are you doing?'
if (name.lastIndexOf('Hi', 0) === 0)
console.log(true);
else console.log(false);

Output:

true

Although, it starts searching from the end. But we are specifying its end at index 0.

Remember that hi and Hi are not the same for this function.

You can have a look in detail more about it here.

Use substring() Function to Check if String Starts With Another String in JavaScript

Instead of specifying another string to check if the string starts with it or not, we can also input the indices range depending on the string’s length.

For instance, we want to check if the string starts with Hi, so the indices are below.

var name = 'Hi John, how are you doing?'
// here the end index is not included means the end index
// would be endIndex-1
console.log(name.substring(0, 2));

Output:

Hi

We can modify it if we look for Hi John, like below.

var name = 'Hi John, how are you doing?'
console.log(name.substring(0, 7));

Output:

Hi John

It is not a good choice to use this function if you are looking for a particular string part somewhere in the middle. The reason is you may not find the expected string on the given indices.

Use Regex’s test() Method to Check if String Starts With Another String in JavaScript

var name = 'Hi John, how are you doing?'
console.log(/^Hi John/.test(name));

Output:

true

It also works case-sensitively. See the following example.

var name = 'Hi John, how are you doing?'
console.log(/^Hi john/.test(name));

Output:

false

You can also use RegExp Object’s methods with different parameters and properties. You can read about it here in detail.

Have you thought of using a custom function to get this feature done? Let’s have a look at the following section.

Use Custom Function With Loop to Check if String Starts With Another String in JavaScript

Example Code:

function startsWithFunction(completeString, starterStr) {
  for (var i = 0; i < starterStr.length; i++) {
    if (completeString[i] !== starterStr[i]) {
      return false;
    }
  }
  return true;
}
console.log(startsWithFunction('Hi John, how are you doing?', 'Hi'));

Output:

true

The startsWithFunction() takes the primary string, Hi John, how are you doing? and another string, Hi, to check if the primary string starts with it or not.

This function matches each character and returns true on success; otherwise, false.

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook

Related Article - JavaScript String