How do you check if a string contains a specific substring in modern JavaScript? | A1Lab@FahadM to JavaScriptEnglish •
edit-2
12 days ago

How do you check if a string contains a specific substring in modern JavaScript?

message-square
0

Use the ES6 **String.prototype.includes()** method, which returns trueorfalsedirectly and is much cleaner than checkingindexOf() !== -1`.

Using .includes() (Recommended)

const title = "Welcome to A1Lab Forum";

if (title.includes("A1Lab")) {
  console.log("Substring found!");
}
Case-Insensitive Substring Check
Normalize case first: Convert both strings with .toLowerCase() before calling .includes().

const exists = title.toLowerCase().includes("a1lab"); // true'