String.prototype.includes
Checks whether a string contains a given substring.
Available since: ES2015
Syntax
str.includes(searchString, position)Parameters
| Parameter | Type | Description |
|---|---|---|
| searchString | string | The substring to look for. |
| position | number | Optional. The position to start searching from; defaults to 0. |
Return value
boolean`true` if the substring is found, `false` otherwise.
Examples
A match and no match:
console.log('Hello, world'.includes('world'));
console.log('Hello, world'.includes('JS'));Output:
true
falseThe search is case-sensitive:
console.log('Hello'.includes('hello'));Output:
falseNotes
To search case-insensitively, convert both strings to the same case with toLowerCase().
See also
- String.prototype.startsWith — Checks whether a string starts with a given substring.
- String.prototype.indexOf — Finds the position of the first occurrence of a substring.
- String.prototype.endsWith — Checks whether a string ends with the given text.
- RegExp.prototype.test — Checks whether a string matches a pattern.
Related recipes
In the tutorial
- String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.