--- title: Array.prototype.includes() slug: Web/JavaScript/Reference/Global_Objects/Array/includes translation_of: Web/JavaScript/Reference/Global_Objects/Array/includes ---
Il metodo includes() determina se un array include un certo elemento, ritornando true o false a seconda del caso
var boolean = array.includes(searchElement[, fromIndex])
searchElementfromIndexsearchElement. Un valore negativo ricerca dal valore di array.length + fromIndex in maniera ascendente. Il valore di default è 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';
if (this == null) {
throw new TypeError('Array.prototype.includes called on null or undefined');
}
var O = Object(this);
var len = parseInt(O.length, 10) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1], 10) || 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;
};
}
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('ES7', '#sec-array.prototype.includes', 'Array.prototype.includes')}} | {{Spec2('ES7')}} | Definizione iniziale. |
| {{SpecName('ESDraft', '#sec-array.prototype.includes', 'Array.prototype.includes')}} | {{Spec2('ESDraft')}} |
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Edge | Opera | Safari |
|---|---|---|---|---|---|---|
| Basic support |
{{CompatChrome(47)}} |
43 | {{CompatNo}} | 14279+ | 34 | 9 |
| Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | {{CompatNo}} |
{{CompatChrome(47)}} |
43 | {{CompatNo}} | 34 | 9 |
{{CompatChrome(47)}} |