1myArray = Array(/*element1, element2, etc...*/);
2
3// If the array 'myArray' contains the element 'valueWeSearch'
4if(myArray.includes(valueWeSearch))
5{
6 // Do something
7}
1var fruits = ["Banana", "Orange", "Apple", "Mango"];
2
3var n = fruits.includes("Mango"); // true
4
5var n = fruits.includes("Django"); // false
1 var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
2
3 // Check if a value exists in the fruits array
4 if(fruits.indexOf("Mango") !== -1){
5 alert("Value exists!")
6 } else{
7 alert("Value does not exists!")
8 }
1[1, 2, 3].includes(2); // true
2[1, 2, 3].includes(4); // false
3[1, 2, 3].includes(1, 2); // false (second parameter is the index position in this array at which to begin searching)
1const fruits = ["Banana", "Orange", "Apple", "Mango"];
2fruits.includes("Mango") // Returns true
1const fruits = ['apple', 'banana', 'mango', 'guava'];
2
3function checkAvailability(arr, val) {
4 return arr.some(arrVal => val === arrVal);
5}
6
7checkAvailability(fruits, 'kela'); // false
8checkAvailability(fruits, 'banana'); // true