--- title: Array.prototype.includes() slug: Web/JavaScript/Reference/Global_Objects/Array/includes translation_of: Web/JavaScript/Reference/Global_Objects/Array/includes original_slug: Web/JavaScript/Referencia/Objectes_globals/Array/includes ---
El mètode includes() determina si un array inclou un element concret, retornant true o false segons s'escaigui.
var boolean = array.includes(elementCercat[, desdePosicio])
elementCercat. Un valor negatiu cercarà el nombre absolut donat de posicions contant des del final de l'array. El seu valor per defecte és 0.Un {{jsxref("Boolean")}}.
[1, 2, 3].includes(2); // true [1, 2, 3].includes(4); // false [1, 2, 3].includes(3, 3); // false [1, 2, 3].includes(3, -1); // true [1, 2, NaN].includes(NaN); // true
if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
'use strict';
var O = Object(this);
var len = parseInt(O.length) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1]) || 0;
var k;
if (n >= 0) {
k = n;
} else {
k = len + n;
if (k < 0) {k = 0;}
}
var currentElement;
while (k < len) {
currentElement = O[k];
if (searchElement === currentElement ||
(searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
return true;
}
k++;
}
return false;
};
}
| Especificació | Estat | Comentaris |
|---|---|---|
| {{SpecName('ES7', '#sec-array.prototype.includes', 'Array.prototype.includes')}} | {{Spec2('ES7')}} | Definició inicial. |
| {{SpecName('ESDraft', '#sec-array.prototype.includes', 'Array.prototype.includes')}} | {{Spec2('ESDraft')}} |
| Característica | Chrome | Firefox (Gecko) | Internet Explorer | Edge | Opera | Safari |
|---|---|---|---|---|---|---|
| Suport bàsic |
{{CompatChrome(47)}} |
43 | {{CompatNo}} | {{CompatNo}} | 34 | 9 |
| Característica | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Suport bàsic | {{CompatNo}} |
{{CompatChrome(47)}} |
43 | {{CompatNo}} | 34 | 9 |
{{CompatChrome(47)}} |