JavaScript includes() to Check Whether a String Contains Text
Use text.includes(term) to test whether a JavaScript string contains a contiguous sequence. It returns true for a match and false otherwise. To get the first match position, use indexOf(). A match at index 0 is valid, so test indexOf(term) !== -1.
Check whether a substring exists
A substring may occur at the beginning, in the middle, or at the end. The method does not check word boundaries: 'catalog'.includes('cat') is true. Use RegExp.test() to test a pattern. Passing a regular expression to includes() throws TypeError. ECMAScript · String.prototype.includes()
console.log('abzcd'.includes('z')); // true
console.log('abzcd'.includes('bz')); // true
console.log('abzcd'.includes('zcd')); // true
console.log('abzcd'.includes('x')); // false
console.log('catalog'.includes('cat')); // true
console.log('catalog'.indexOf('cat')); // 0
console.log('catalog'.indexOf('cat') !== -1); // true
try {
'abc'.includes(/c/);
} catch (error) {
console.log(error.name); // TypeError
}
Start searching at position
text.includes(term, position) starts at position, which defaults to 0. The method converts the value to an integer and clamps it between 0 and text.length. Starting at 2 includes index 2 and skips any match that begins before it.
const text = 'abzcd';
console.log(text.includes('z', 2)); // true
console.log(text.includes('z', 3)); // false
console.log(text.slice(3)); // 'cd'
console.log(text.includes('z', 2.9)); // true
console.log(text.includes('z', -1)); // true
console.log(text.includes('z', 99)); // false
console.log('A🙂Z'.includes('🙂', 1)); // true
console.log('A🙂Z'.includes('🙂', 2)); // false
Positions count UTF-16 code units. In 'A🙂Z', the emoji begins at index 1 and Z is at index 3. For a method whose second argument sets an end boundary, see the endsWith() guide on checking a suffix.
Handle case and empty queries
Matching is case-sensitive. For simple case-insensitive English matching, lowercase both the text and the query. Search across languages needs its own rules for case and accents.
console.log('Blue Whale'.includes('blue')); // false
console.log('Blue Whale'.toLowerCase().includes('BLUE'.toLowerCase())); // true
console.log('abc'.includes('')); // true
console.log('abc'.includes()); // false
console.log('undefined'.includes()); // true
An empty string always matches. Decide whether an empty search box should show everything or prompt the user first. Omitting the search argument searches for 'undefined'. Check that optional inputs are present and have the expected type before calling the method.
Change the starting position in a standalone example
Save the code below as string-includes.html in UTF-8 and open it in a browser. The initial result is true because abzcd contains z. Enable the starting position at 2 and the search still matches in zcd. Change it to 3 to search cd and get false.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Substring search</title>
</head>
<body>
<label for="text">Original string</label>
<input id="text" type="text" value="abzcd">
<label for="term">Search term</label>
<input id="term" type="text" value="z">
<label><input id="use-start" type="checkbox"> Use starting position</label>
<label for="start">Starting position</label>
<input id="start" type="number" min="0" step="1" value="2" disabled>
<pre id="output" aria-live="polite"></pre>
<script>
const text = document.querySelector('#text');
const term = document.querySelector('#term');
const useStart = document.querySelector('#use-start');
const start = document.querySelector('#start');
const output = document.querySelector('#output');
function show() {
start.disabled = !useStart.checked;
const position = Number(start.value);
if (useStart.checked && (start.value.trim() === '' ||
!Number.isInteger(position) || position < 0)) {
output.textContent = "Enter a non-negative integer for the starting position.";
return;
}
const value = text.value;
const query = term.value;
const part = useStart.checked ? value.slice(position) : value;
const found = useStart.checked
? value.includes(query, position) : value.includes(query);
output.textContent = `Searched text: ${JSON.stringify(part)}\n` +
`Search term: ${JSON.stringify(query)}\n` +
`includes() result: ${found}`;
}
text.addEventListener('input', show);
term.addEventListener('input', show);
useStart.addEventListener('change', show);
start.addEventListener('input', show);
show();
</script>
</body>
</html>
The example displays a message for a blank, negative, or fractional position while the option is enabled. Turn it off to search the whole string again. Clear the query to observe the empty match.




