How to check if an element exists in an array in Javascript
27 Dec 2014The below function can efficiently check whether an element exists in the array or not
if (!Array.prototype.elemetExists) {
Array.prototype.elemetExists = function (string) {
return !!~this.indexOf(string);
};
}
//usage
var fruits = ['Apple' , 'Banana' , 'Orange' , 'Mango'];
console.log(fruits.elemetExists('Banana')); //true
console.log(fruits.elemetExists('Lichi')); //false