String.includes()
Checks if a string contains another string (true/false).
string.includes(searchString, position?)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Checks if a string contains another string (true/false).
String.prototype.includes() is a case-sensitive method that determines whether a string contains a specific sequence of characters, returning a boolean value. It follows the SameValueZero algorithm for comparison, which for string primitives is functionally equivalent to strict equality of characters. While more readable than the traditional 'indexOf() !== -1' pattern, it does not support regular expressions; attempting to pass a RegExp object as the search string will throw a TypeError. Performance is generally O(n) relative to the length of the string, though modern engines use highly optimized search algorithms like Boyer-Moore or Horspool. Note that searching for an empty string ('') always returns true, even if the starting position exceeds the string length. This method was introduced in ES6 (ECMAScript 2015) and is not supported in Internet Explorer.
Quick reference
Syntax
string.includes(searchString, position?)
Inputs
Parameters
See it in practice
Examples
Basic Substring Detection
const sentence = 'The quick brown fox jumps over the lazy dog.';
const word = 'fox';
console.log(sentence.includes(word));true
Determines if the substring 'fox' exists anywhere within the target string.
Searching with a Start Position
const text = 'To be, or not to be, that is the question.';
// Check if 'To' exists starting from index 1
console.log(text.includes('To', 1));false
The second argument specifies the index to begin the search. Since 'To' starts at index 0, searching from index 1 returns false.
Case-Insensitive Validation
const email = 'User@Example.com';
const search = 'user';
const isValid = email.toLowerCase().includes(search.toLowerCase());
console.log(isValid);true
Because includes() is case-sensitive, both strings must be normalized (e.g., to lowercase) to perform a case-insensitive match.
Debug faster
Common Errors
TypeError
Cause: Passing a Regular Expression as the first argument instead of a string.
Fix: Use a plain string for .includes() or use RegExp.prototype.test() if pattern matching is required.
'hello world'.includes(/hello/);LogicError
Cause: Assuming the method is case-insensitive, leading to missed matches.
Fix: Always normalize both the source and the search term using .toLowerCase() or .toUpperCase().
const status = 'Active';
if (status.includes('active')) { /* Will not execute */ }Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Checks if a string contains another string (true/false).
searchString: Substring to look for. position: Index to start searching from.
TypeError: Use a plain string for .includes() or use RegExp.prototype.test() if pattern matching is required. LogicError: Always normalize both the source and the search term using .toLowerCase() or .toUpperCase().