The includes()
method performs a case-sensitive search to determine whether one string may be found within another string, returning true
or false
as appropriate.
The includes()
method performs a case-sensitive search to determine whether one string may be found within another string, returning true
or false
as appropriate.
includes(searchString) includes(searchString, position)
searchString
A string to be searched for within str
. Cannot be a regex.
position
Optional
The position within the string at which to begin searching for searchString
. (Defaults to 0
.)
true
if the search string is found anywhere within the given string; otherwise, false
if not.
TypeError
If searchString
is a regex.
This method lets you determine whether or not a string includes another string.
The includes()
method is case sensitive. For example, the following expression returns false
:
'Blue Whale'.includes('blue') // returns false
You can work around this constraint by transforming both the original string and the search string to all lowercase:
'Blue Whale'.toLowerCase().includes('blue') // returns true
const str = 'To be, or not to be, that is the question.' console.log(str.includes('To be')) // true console.log(str.includes('question')) // true console.log(str.includes('nonexistent')) // false console.log(str.includes('To be', 1)) // false console.log(str.includes('TO BE')) // false console.log(str.includes('')) // true
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
includes |
41 |
12 |
40
18-48
|
No |
28 |
9 |
41 |
41 |
40
18-48
|
28 |
9 |
4.0 |
1.0 |
4.0.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes