How to check if value exists in array JavaScript?
1. Using includes()
const fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.includes("Mango"));
2. Using indexof()
const fruits = ["Banana", "Orange", "Apple", "Mango"];
if(fruits.indexOf("Orange") !== -1) {
alert("Yes, the value exists!")
} else {
alert("No, the value is absent.")
}
BY Best Interview Question ON 25 Aug 2022