myArray = Array(/*element1, element2, etc...*/);
// If the array 'myArray' contains the element 'valueWeSearch'
if(myArray.includes(valueWeSearch))
{
// Do something
}
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango");
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var n = fruits.includes("Mango"); // true
var n = fruits.includes("Django"); // false
var army= ["Marcos", "DeltaForce", "Seals", "SWAT", "HeadHunters"];
if(army.indexOf("Marcos") !== -1) {
console.log("Yes, the value exists!")
}else{
console.log("No, the value is absent.")
}
array.includes('element that need to be checked') //returns true or false
myArr?.[index]
var contains = function(needle) {
// Per spec, the way to identify NaN is that it is not equal to itself
var findNaN = needle !== needle;
var indexOf;
if(!findNaN && typeof Array.prototype.indexOf === 'function') {
indexOf = Array.prototype.indexOf;
} else {
indexOf = function(needle) {
var i = -1, index = -1;
for(i = 0; i < this.length; i++) {
var item = this[i];
if((findNaN && item !== item) || item === needle) {
index = i;
break;
}
}
return index;
};
}
return indexOf.call(this, needle) > -1;
};