JavaScript: Check if a String Starts or Ends With a Substring

Updated: February 28, 2023 By: Frienzied Flame Post a comment

This concise, straightforward article shows a couple of different ways to check if a given string starts or ends with a substring in JavaScript.

Using startsWith() and endsWith()

Overview

You can use the built-in string methods startsWith() and endsWith() to check if a string starts or ends with a particular substring. These methods return a boolean value of true or false based on the result of the check.

The syntax for the startsWith() method is:

string.startsWith(searchString, position)

Where:

  • searchString: The substring to search for at the beginning of the string
  • position: An optional parameter that specifies the position within the string to begin the search. If not specified, the default value is 0.

And here is the syntax for the endsWith() method:

string.endsWith(searchString, endPosition)

Where:

  • searchString: The substring to search for at the end of the string
  • endPosition: An optional parameter that specifies the number of characters from the end of the string to include in the search. If not specified, the default value is the length of the string.

Examples

Let’s have a look at some examples to understand how to use these methods in real-world scenarios.

Checking if a string starts with a substring:

const str = "Welcome to SlingAcademy.com";
const prefix = "Welcome to";

console.log(str.startsWith(prefix)); 

Output:

true

Checking if a string ends with a substring:

const str = "Welcome to SlingAcademy.com";
const suffix = "SlingAcademy.com";

console.log(str.endsWith(suffix)); 

Output:

true

Using startsWith() with the position parameter:

const str = "Sling Academy";
const prefix = "Academy";

console.log(str.startsWith(prefix, 6)); 

Output:

true

Using the endsWith() method with the endPosition parameter:

const str = "Welcome to Sling Academy";
const suffix = "Sling";

console.log(str.endsWith(suffix, 16));

Output:

true

Using Regular Expressions

Regular expressions are another powerful tool in JavaScript for matching and manipulating strings, including checking if a string starts or ends with a particular substring. In fact, regular expressions can provide more flexibility and control than using the startsWith() and endsWith() methods, as they allow for more complex pattern matching.

To check if a string starts with a particular substring using regular expressions, you can use the test() method of the regular expression object like this:

const str = "John Doe is a software engineer";
const regex = /^John/;

console.log(regex.test(str)); 

Output:

true

Note that the ^ character in the regular expression indicates the start of the string.

Similarly, to check if a string ends with a particular substring using regular expressions, you can use the $ character to indicate the end of the string, as shown below:

const str = "Hello, welcome to Sling Academy";
const regex = /Academy$/;

console.log(regex.test(str)); 

Output:

true

Afterword

You’ve learned more than one technique to check whether a given string starts/ends with a substring or not. Which one would you use? Please let us know by leaving a comment. Happy coding and have a nice day!