includes()
ES6+Determines whether one string may be found within another string.
Syntax
string.includes(searchString, position)Parameters
searchString string The string to search for
position number optionalPosition to start searching from
Return Value
true if the string is found, otherwise false
Examples
const str = 'Hello World';
console.log(str.includes('World'));
console.log(str.includes('world')); 📌 When to Use
Use includes() for simple substring existence checks when you only need a boolean result. It provides cleaner and more readable code than indexOf() !== -1 for conditional logic.
⚠️ Common Mistakes
Forgetting that includes() is case-sensitive - "Hello".includes("hello") returns false. Use toLowerCase() on both strings for case-insensitive search.
Using includes() when you actually need the position - includes() returns boolean only, use indexOf() if you need the position.
Not understanding that includes() does not support regex patterns - use match() or search() for pattern matching.
✅ Best Practices
Use includes() in if statements for cleaner conditional code: if (str.includes(search)) instead of if (str.indexOf(search) !== -1).
Combine with toLowerCase() for case-insensitive checks: str.toLowerCase().includes(search.toLowerCase()).
Use the position parameter when you want to search from a specific index, useful for finding subsequent occurrences.
⚡ Performance Notes
includes() and indexOf() have nearly identical performance as they both scan the string from the starting position. For multiple substring checks, consider building a Set or using regex alternation for better performance.
🌍 Real World Example
Content Filtering and Search Features
includes() is commonly used for search functionality, filtering lists, content moderation, and checking if URLs or strings contain specific patterns.
// Simple search filter for products
function searchProducts(products, query) {
const lowerQuery = query.toLowerCase();
return products.filter(product =>
product.name.toLowerCase().includes(lowerQuery) ||
product.description.toLowerCase().includes(lowerQuery)
);
}
const products = [
{ name: 'iPhone 15', description: 'Apple smartphone' },
{ name: 'Galaxy S24', description: 'Samsung smartphone' },
{ name: 'MacBook Pro', description: 'Apple laptop' }
];
console.log(searchProducts(products, 'apple'));
// Returns iPhone 15 and MacBook Pro
// Check if URL is from allowed domains
function isAllowedUrl(url, allowedDomains) {
return allowedDomains.some(domain =>
url.toLowerCase().includes(domain.toLowerCase())
);
}
const allowed = ['example.com', 'trusted.org'];
console.log(isAllowedUrl('https://api.example.com/data', allowed)); // true
console.log(isAllowedUrl('https://malicious.com/data', allowed)); // false
// Content moderation - check for banned words
function containsBannedWords(text, bannedWords) {
const lowerText = text.toLowerCase();
return bannedWords.some(word =>
lowerText.includes(word.toLowerCase())
);
}
const banned = ['spam', 'scam', 'fake'];
console.log(containsBannedWords('This is a scam!', banned)); // true