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
searchString
; defaults to 0.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 6 specification and may not be available in all JavaScript implementations yet. However, you can easily polyfill this method:
if (!str.includes) { str.includes = function() {'use strict'; return str.indexOf.apply(this, arguments) !== -1; }; }
In Firefox 18 - 39, the name of this method was contains()
. It was renamed to includes()
in 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 str.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' str.contains()
implementation to break. As a result, this change was disabled in Firefox 17 and str.contains()
was available one version later, in Firefox 18.
MooTools 1.3 forces its own version of str.contains()
, so websites relying on it should not break. However, you should note that MooTools 1.3 signature and ECMAScript 6 signatures for this method differ (on the second argument). Later, MooTools 1.5+ changed the signature to match the ES6 standard.
Created by Mozilla Contributors, license: CC-BY-SA 2.5