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. When the substring is absent, !str.includes(searchText) is the usual guard; see JavaScript logical NOT for how unary ! differs from !==.
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.
The complete JavaScript indexOf guide also covers
starting positions, repeated matches, and the separate array method.
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. See JavaScript toLowerCase for search normalization, immutability, and locale-aware casing.
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.
The JavaScript pattern-matching guide
compares search(), match(), matchAll(), replace(), and split().
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.
How do I check for spaces or other whitespace?
The JavaScript whitespace-check guide distinguishes a literal space from tabs, newlines, Unicode separators, and an all-whitespace string.
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.

