The includes() method
const text = "the quick brown fox";
console.log(text.includes("quick")); // true
console.log(text.includes("cat")); // false
One thing to remember: includes() is case-sensitive, so “Quick” would not match “quick.”
Case-insensitive search
const found = text.toLowerCase().includes("QUICK".toLowerCase());
console.log(found); // true
Lower-casing both sides removes the case mismatch.
Find the position with indexOf()
if (text.indexOf("brown") !== -1) {
console.log("found it");
}
indexOf() returns the position of the match, or -1 if it is not there.
Pattern matching with a regular expression
console.log(/quick/i.test(text)); // true
The i flag makes the search case-insensitive, and regex is the way to go when you need a pattern rather than an exact word.
Frequently asked questions
includes() or indexOf()?
Use includes() when you only need a yes/no answer — it reads better. Use indexOf() when you also need the position of the match.
How do I check the start or end of a string?
Use startsWith("...") and endsWith("...").
Build a solid base with our free JavaScript course.