String.prototype.includes
Determines whether one string may be found within another string, returning true or false
Syntax
string.includes(searchString, position?)Parameters
| Parameter | Type | Description |
|---|---|---|
| searchString | string | A string to be searched for |
| position | number | Position to begin searching from |
Return Value
true if the search string is found, false otherwise
Examples
const str = 'Hello World';
console.log(str.includes('World')); // true
console.log(str.includes('world')); // falseconst url = 'https://example.com/api/users';
if (url.includes('/api/')) {
console.log('API endpoint detected');
}const sentence = 'The quick brown fox';
console.log(sentence.includes('quick', 5)); // falseUnderstanding String.prototype.includes
The String.prototype.includes method in JavaScript determines whether one string may be found within another string, returning true or false. It belongs to the String object and is one of the most widely used methods for working with string values in modern JavaScript and TypeScript applications.
The method signature is string.includes(searchString, position?). It accepts 2 parameters: searchString, position. When called, it returns true if the search string is found, false otherwise. Understanding when and how to use includes() helps you write more expressive, readable code.
Common use cases for String.prototype.includes include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like string-indexof, string-startswith, string-endswith, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for String.prototype.includes is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
String.prototype.indexOfReturns the index of the first occurrence of a specified value in a string, or -1 if not found
String.prototype.startsWithDetermines whether a string begins with the characters of a specified string
String.prototype.endsWithDetermines whether a string ends with the characters of a specified string
Array.prototype.includesDetermines whether an array includes a certain value among its entries, returning true or false
More String Methods
Other methods in the String object
Related Tools
More String Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.