How to Check and Extract Substrings in JavaScript

A substring is a smaller piece of text inside a larger one. One of the most common tasks when working with strings in JavaScript is checking whether a substring exists. And if it does, you might want to extract it or use it somehow.

Let’s break this down into two parts: checking and extracting.

Checking if a String Contains a Substring

JavaScript gives you several ways to do this, but the simplest and most readable is .includes().

If the substring is found anywhere in the string, .includes() returns true. If not, it returns false.

Example of checking if a string contains a substring
(click on the image to open in a new tab)

Note: This method is case-sensitive, so “javascript” would return false  in the above example.

You can also use includes() to search for a substring within a string by providing a second parameter to specify the starting index:

Here, we start searching from index 7 and on.
(click on the image to open in a new tab)

Starting the search from a different index can return false:

Starting the search from index 10 would return FALSE, since the substring is only partially included in this case.
Note: The includes() method  does not provide information on where the substring is situated in the string or how often does it appear.

If you need information on where the substring resides in the string, other methods, such as the indexOf() method might be more suitable.

With indexOf(searchElement), we can find the index which is the substing’s starting point.
(click on the image to open in a new tab)

Extracting a Substring from a String

Once you know a substring exists, you can extract it using .slice().

.slice() needs start and end parameters to grab the exact portion of the string.
(click on the image to open in a new tab)

Here’s how you’d extract “JavaScript” from the sentence above:

You’re using the start and end positions to grab the exact portion of the string.
(click on the image to open in a new tab)

Use Case: Find and Print a Keyword in a Sentence

For this example, we scan a sentence; if we find the substring ‘pioneer’, we extract it for use.

Here, we want to find, extract, and print the word “pioneer” from the sentence.
(click on the image to open in a new tab)

The substring is print as expected:

Once the substring is extracted, is printed on the console.
(click on the image to open in a new tab)

What’s Happening Here?

  • You start with a string:
    bio = “Ada Lovelace was a pioneer in computing.”;
  • You want to check if the word “pioneer” is in the sentence.

Step 1: Check if it’s there

We use the includes() method.
(click on the image to open in a new tab)
  1. Returns true if the word is found.
  2. This is a quick way to avoid unnecessary logic if the word isn’t present at all.

Step 2: Get its starting position

Now we use the indexOf() method to get the starting position.
(click on the image to open in a new tab)
  1. This tells you the index (character position) where the word starts.
  2. In this case, it returns 22 because “pioneer” begins at the 22nd character in the string.

Step 3: Slice out the exact word

The slice() method let us define the exact substring.
(click on the image to open in a new tab)

  1. You use .slice() to extract just the word, using:
    • start = 22
    • end = 22 + 729, because “pioneer” is 7 characters long.
  2. The result is “pioneer”, which you can now use however you want.

In Summary

  • Use .includes() when you just want to check if a substring exists.
  • Use .indexOf() to find where it starts.
  • Use .slice() or .substring() to extract it once you know the position.

These are simple yet powerful tools to check and manipulate text instantly—perfect for searches, filters, and formatting.

Although my blog doesn’t support comments, feel free to reply via email or X.