The includes()
method determines whether one string may be found within another string, returning true
or false
as appropriate.
str.includes(searchString[, position])
searchString
position
Optional
searchString
. (defaults to 0).true
if the search string is found anywhere within the given string; otherwise, false
if not.
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
includes()
var 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
This method has been added to the ECMAScript 2015 specification and may not be available in all JavaScript implementations yet. However, you can easily polyfill this method:
if (!String.prototype.includes) { Object.defineProperty(String.prototype, 'includes', { value: function(search, start) { if (typeof start !== 'number') { start = 0 } if (start + search.length > this.length) { return false } else { return this.indexOf(search, start) !== -1 } } }) }
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String.prototype.includes' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of 'String.prototype.includes' in that specification. | Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 41 | Yes | 40
|
No | Yes | 9 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 40
|
Yes | 9 | Yes |
Server | |
---|---|
Node.js | |
Basic support | 4.0.0 |
In Firefox 18 - 39, the name of this method was contains()
. It was renamed to includes()
in bug 1102219 due to the following reason:
It's been reported that some websites using MooTools 1.2 broke on Firefox 17. This version of MooTools checks whether String.prototype.contains()
exists and, if it doesn't, MooTools adds its own function. With the introduction of this function in Firefox 17, the behavior of that check changed in a way that causes code based on MooTools' String.prototype.contains()
implementation to break. As a result, the implementation was disabled in Firefox 17 and String.prototype.contains()
was available one version later, in Firefox 18, when outreach to MooTools was leading to the release of MooTools version 1.2.6.
MooTools 1.3 forces its own version of String.prototype.contains()
, so websites relying on it should not break. However, you should note that MooTools 1.3 signature and ECMAScript 2015 signatures for this method differ (on the second argument). Later, MooTools 1.5+ changed the signature to match the ES2015 standard.
In Firefox 48, String.prototype.contains()
has been removed. Use String.prototype.includes()
only.
Array.prototype.includes()
TypedArray.prototype.includes()
String.prototype.indexOf()
String.prototype.lastIndexOf()
String.prototype.startsWith()
String.prototype.endsWith()
© 2005–2018 Mozilla Developer Network and individual 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