String.prototype.includes()
includes(searchString[, position]) Returns:
boolean · Added in vES6 · Updated March 13, 2026 · String Methods string includes es6
The includes() method determines whether one string can be found within another. It returns true if the substring exists anywhere in the string, and false if not. This method is case-sensitive and was introduced in ES6 as a more readable alternative to checking indexOf() !== -1.
One notable behavior: includes() returns true when searching for an empty string, since every string contains the empty string at every position.
Syntax
str.includes(searchString);
str.includes(searchString, position);
TypeScript Signature
interface String {
includes(searchString: string, position?: number): boolean;
}
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
searchString | string | — | The substring to search for. Cannot be a regex. All non-regex values are coerced to strings. |
position | number | 0 | The index at which to begin searching. Defaults to 0. |
Examples
Basic usage
const message = "Hello, world!";
console.log(message.includes("Hello"));
// true
console.log(message.includes("hello"));
// false (case-sensitive)
console.log(message.includes("world"));
// true
console.log(message.includes("JavaScript"));
// false
// Empty string always returns true
console.log(message.includes(""));
// true
Using the position parameter
const text = "The quick brown fox jumps over the lazy dog";
console.log(text.includes("fox", 10));
// true — search starts at index 10
console.log(text.includes("fox", 20));
// false — "fox" is at index 16, which is before position 20
// Searching from the beginning explicitly
console.log(text.includes("The", 0));
// true
Practical examples
// Validating user input
const email = "user@example.com";
const hasAtSymbol = email.includes("@");
console.log(hasAtSymbol);
// true
// Checking for forbidden words
const comment = "This is a great tutorial!";
const hasSpam = comment.includes("buy now") || comment.includes("click here");
console.log(hasSpam);
// false
// Case-insensitive search
const searchIn = "JavaScript Fundamentals";
const searchFor = "javascript";
const found = searchIn.toLowerCase().includes(searchFor.toLowerCase());
console.log(found);
// true
Common Patterns
Replacing indexOf() checks
// Old way
if (str.indexOf("error") !== -1) {
console.log("Found error");
}
// Modern way
if (str.includes("error")) {
console.log("Found error");
}
Chaining with other string methods
const url = "https://example.com/api/users";
const isSecure = url.startsWith("https") && url.includes("example");
console.log(isSecure);
// true
Error Handling
Unlike some string methods, includes() does not throw an error when given undefined or no argument. Instead, it converts the value to a string:
console.log("undefined".includes(undefined));
// true — searches for the string "undefined"
console.log("hello".includes());
// false — searches for the string "undefined"
However, passing a regex throws a TypeError:
try {
"test".includes(/test/);
} catch (e) {
console.log(e.name);
// TypeError
}
When to Use
Use includes() when you need to:
- Check for substring presence with a simple boolean result
- Validate user input (email format, forbidden words)
- Make code more readable compared to
indexOf() !== -1
When Not to Use
Avoid includes() when:
- You need the position of the match — use
indexOf()instead - You need case-insensitive matching — convert both strings to lowercase first
- You need pattern matching — use
match()orsearch()with regex
See Also
- string::startsWith — Check prefix
- string::endsWith — Check suffix