-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
18: String Methods: includes, startsWith, endsWith
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).
There are no comments for now.