Skip to Content
Course content

18: String Methods: includes, startsWith, endsWith

Click on the "Edit" button in the top corner of the screen to edit your slide content.

When you're dealing with user input or parsing data from an API, you rarely need to know the *exact* value of a string. More often, you just need to know if a string contains a certain piece of information. That's where includes(), startsWith(), and endsWith() come in. They return a simple boolean—true or false—which makes them perfect for if statements.

To see these in action, let's build a simple "Message Router." Imagine we're building a system that looks at incoming support tickets and tags them automatically so they go to the right department.

Catching the Urgent Stuff with startsWith

First, I want to identify any messages that start with the word "URGENT". If a user is shouting at the start of their message, it probably needs to skip the queue. I'll use startsWith() here because the position of the word matters; if "urgent" is buried in the middle of a paragraph, it's less of a priority than if it's the very first thing written.

const message = "URGENT: My database is down!";

if (message.startsWith("URGENT")) {
  console.log("Routing to Critical Response Team...");
}

Filtering out the Noise with endsWith

Next, I want to filter out messages coming from known spam domains. Since the domain is always at the end of an email address, endsWith() is the most efficient tool for the job. I don't care what the username is; I only care about the suffix.

const senderEmail = "marketing-bot@spam-central.net";

if (senderEmail.endsWith("@spam-central.net")) {
  console.log("Moving message to Junk folder.");
}

Hunting for Keywords with includes

Now, some messages aren't necessarily urgent or from spam, but they might mention a specific product. Let's say we want to tag any message that mentions "billing" or "invoice" regardless of where those words appear. This is where includes() shines. It doesn't care about the position; it just scans the whole string.

const body = "I have a question regarding my latest invoice for January.";

if (body.includes("invoice") || body.includes("billing")) {
  console.log("Routing to Accounts Receivable...");
}

Dealing with the Case-Sensitivity Trap

Here is where I usually trip up when I'm coding this quickly. I'll show you the mistake I just made in my head. I tried to check for a "Refund" request like this:

const userRequest = "I want a REFUND immediately!";
if (userRequest.includes("refund")) {
  console.log("Routing to Billing...");
}
// Result: This returns false!

JavaScript strings are case-sensitive. "REFUND" is not the same as "refund". In a real-world app, you can't trust the user to use the correct casing. To fix this, I always normalize the string by converting it to lowercase before performing the check. It's a small step, but it saves you from a lot of "why isn't this working?" debugging sessions.

const userRequest = "I want a REFUND immediately!";
const normalizedRequest = userRequest.toLowerCase();

if (normalizedRequest.includes("refund")) {
  console.log("Routing to Billing...");
}
// Result: This returns true. Much better.



📋 Practical Task

Build a File Upload Validator

You are building a file upload system that only accepts specific image files. Write a script that checks a variable called fileName and applies the following logic:

  • If the file starts with "IMG_" AND ends with either ".jpg" or ".png", log "File accepted".
  • Otherwise, log "Invalid file format".

Test your code with these three cases: "IMG_vacation.jpg" (should be accepted), "IMG_profile.gif" (should be rejected), and "notes.txt" (should be rejected).

Rating
0 0

There are no comments for now.

to be the first to leave a comment.