String includes()
String.prototype.includes() checks whether a string contains a substring, while startsWith() and endsWith() cover common boundary checks. These methods are usually clearer than older indexOf comparisons.
Overview
String.prototype.includes() checks whether a string contains a substring, while startsWith() and endsWith() cover common boundary checks. These methods are usually clearer than older indexOf comparisons.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 41 | 12 | 40 | 9 | 41 | 9 | |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 2 item(s)
Removed
- This feature was removed in a later browser version (48)
Implementation note
- Previously available under a different name: contains (18)
Syntax
JAVASCRIPT
const url = 'https://example.com/api/users';
url.includes('api'); // true
url.startsWith('https'); // true
url.endsWith('/users'); // true
// Specifying the search start position
'hello world'.includes('world', 6); // true Live demo
Use cases
Keyword checks
Detect whether user input, labels, or messages contain a particular term before branching UI behavior.
Prefix and suffix validation
Check file extensions, secure URLs, or naming conventions without manual slicing logic.
Cautions
- String matching is case-sensitive, so normalize text first when searches should ignore case.
- These helpers do not understand linguistic rules such as stemming or locale-aware comparison.
Accessibility
- If text matching drives warnings or validation, make the feedback explicit and readable rather than relying on silent string checks.
Related links
Powered by web-features