To check whether a JavaScript string contains a substring, use includes(). It returns true when the substring is found and false when it is not found. For older patterns, indexOf() is still common, and regular expressions help when the match is pattern-based.
String contains checks are case-sensitive by default, so normalize the text when user input should match regardless of case.
Environment: Node.js v20.18.2. After each runnable snippet, the following paragraph states the expected console output (order and values).
Method 1: Check String Contains with includes()
console.log("contains-basic:", "JavaScript string contains".includes("string"));You should see one line logging contains-basic: true.
includes() is the clearest method for a plain substring check.
Method 2: Start Searching from a Position
console.log("contains-position:", "JavaScript string contains".includes("Java", 1));You should see one line logging contains-position: false.
The search starts at index 1, so it no longer sees Java at the beginning.
Method 3: Check Contains with indexOf()
console.log("contains-indexof:", "JavaScript".indexOf("Script") !== -1);You should see one line logging contains-indexof: true.
indexOf() returns the first matching index, or -1 when the substring is missing.
Method 4: Case-Insensitive Contains Check
const text = "JavaScript String Contains";
console.log(text.toLowerCase().includes("string"));You should see one line logging true.
Convert both values to lowercase or uppercase before comparing.
Method 5: Use Regex for Pattern Contains
console.log(/\d{3}/.test("Order 123"));You should see one line logging true.
Use a regular expression when the match is a pattern rather than exact text.
Common Questions About JavaScript String Contains
What is the best way to check if a string contains text?
Use str.includes(searchText) for plain substring checks.
Is includes case-sensitive?
Yes. Normalize both strings with toLowerCase() or toUpperCase() for case-insensitive search.
What did developers use before includes()?
indexOf(searchText) !== -1 is the older common pattern.
Summary
Use includes() to check whether a JavaScript string contains a substring. Use the optional position argument when the search should start later, indexOf() when you need the match index or older style, and regular expressions for pattern-based contains checks. Normalize case when user input should match case-insensitively.

