Sling Academy
Home/JavaScript/JavaScript: Check if a String Starts or Ends With a Substring

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

Last updated: February 28, 2023

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!

Next Article: JavaScript: 3 Ways to Create Multiline Strings

Previous Article: 3 Ways to Validate an Email Address in JavaScript

Series: JavaScript Strings

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration