javascript check if value exists in array code example
Example 1: check if array does not contain value javascript
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var n = fruits.includes("Mango");
var n = fruits.includes("Django");
Example 2: javascript array contains
var colors = ["red", "blue", "green"];
var hasRed = colors.includes("red");
var hasYellow = colors.includes("yellow");
Example 3: javascript method to see if item exists in array
var fruits = ["Apple", "Banana", "Mango", "Orange", "Papaya"];
if(fruits.indexOf("Mango") !== -1){
alert("Value exists!")
} else{
alert("Value does not exists!")
}
Example 4: check value exist in array javascript
[1, 2, 3].includes(2);
[1, 2, 3].includes(4);
[1, 2, 3].includes(1, 2);
Example 5: not in array js
!array.includes("element")
Example 6: Checking whether a value exists in an array javascript
const fruits = ['apple', 'banana', 'mango', 'guava'];
function checkAvailability(arr, val) {
return arr.some(arrVal => val === arrVal);
}
checkAvailability(fruits, 'kela');
checkAvailability(fruits, 'banana');